home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / dsp / dspkgctr.z / dspkgctr / gcc / combine.c < prev    next >
C/C++ Source or Header  |  1992-06-08  |  90KB  |  2,820 lines

  1. /* Optimize by combining instructions for GNU compiler.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4.    $Id: combine.c,v 1.5 91/10/23 16:07:43 pete Exp $
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 1, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22.  
  23. /* This module is essentially the "combiner" phase of the U. of Arizona
  24.    Portable Optimizer, but redone to work on our list-structured
  25.    representation for RTL instead of their string representation.
  26.  
  27.    The LOG_LINKS of each insn identify the most recent assignment
  28.    to each REG used in the insn.  It is a list of previous insns,
  29.    each of which contains a SET for a REG that is used in this insn
  30.    and not used or set in between.  LOG_LINKs never cross basic blocks.
  31.    They were set up by the preceding pass (lifetime analysis).
  32.  
  33.    We try to combine each pair of insns joined by a logical link.
  34.    We also try to combine triples of insns A, B and C when
  35.    C has a link back to B and B has a link back to A.
  36.  
  37.    LOG_LINKS does not have links for use of the CC0.  They don't
  38.    need to, because the insn that sets the CC0 is always immediately
  39.    before the insn that tests it.  So we always regard a branch
  40.    insn as having a logical link to the preceding insn.
  41.  
  42.    We check (with use_crosses_set_p) to avoid combining in such a way
  43.    as to move a computation to a place where its value would be different.
  44.  
  45.    Combination is done by mathematically substituting the previous
  46.    insn(s) values for the regs they set into the expressions in
  47.    the later insns that refer to these regs.  If the result is a valid insn
  48.    for our target machine, according to the machine description,
  49.    we install it, delete the earlier insns, and update the data flow
  50.    information (LOG_LINKS and REG_NOTES) for what we did.
  51.  
  52.    To simplify substitution, we combine only when the earlier insn(s)
  53.    consist of only a single assignment.  To simplify updating afterward,
  54.    we never combine when a subroutine call appears in the middle.
  55.  
  56.    Since we do not represent assignments to CC0 explicitly except when that
  57.    is all an insn does, there is no LOG_LINKS entry in an insn that uses
  58.    the condition code for the insn that set the condition code.
  59.    Fortunately, these two insns must be consecutive.
  60.    Therefore, every JUMP_INSN is taken to have an implicit logical link
  61.    to the preceding insn.  This is not quite right, since non-jumps can
  62.    also use the condition code; but in practice such insns would not
  63.    combine anyway.  */
  64.  
  65. #include <stdio.h>
  66.  
  67. #include "config.h"
  68. #include "rtl.h"
  69. #include "flags.h"
  70. #include "regs.h"
  71. #if defined( _INTELC32_ )
  72. #include "bblock.h"
  73. #include "iconfig.h"
  74. #else
  75. #include "basic-block.h"
  76. #include "insn-config.h"
  77. #endif
  78. #include "recog.h"
  79.  
  80. #if ! defined( _MSDOS )
  81. #define max(A,B) ((A) > (B) ? (A) : (B))
  82. #define min(A,B) ((A) < (B) ? (A) : (B))
  83. #endif
  84.  
  85. /* It is not safe to use ordinary gen_lowpart in combine.
  86.    Use gen_lowpart_for_combine instead.  See comments there.  */
  87. #define gen_lowpart dont_use_gen_lowpart_you_dummy
  88.  
  89. /* Number of attempts to combine instructions in this function.  */
  90.  
  91. static int combine_attempts;
  92. static int distrib_attempts;
  93.  
  94. /* Number of attempts that got as far as substitution in this function.  */
  95.  
  96. static int combine_merges;
  97. static int distrib_merges_1, distrib_merges_2;
  98.  
  99. /* Number of instructions combined with added SETs in this function.  */
  100.  
  101. static int combine_extras;
  102.  
  103. /* Number of instructions combined in this function.  */
  104.  
  105. static int combine_successes;
  106. static int distrib_successes;
  107.  
  108. /* Totals over entire compilation.  */
  109.  
  110. static int total_attempts, total_merges, total_extras, total_successes;
  111. static int total_distrib_attempts, total_distrib_merges_1, total_distrib_merges_2, total_distrib_successes;
  112.  
  113.  
  114. /* Vector mapping INSN_UIDs to cuids.
  115.    The cuids are like uids but increase monononically always.
  116.    Combine always uses cuids so that it can compare them.
  117.    But actually renumbering the uids, which we used to do,
  118.    proves to be a bad idea because it makes it hard to compare
  119.    the dumps produced by earlier passes with those from later passes.  */
  120.  
  121. static int *uid_cuid;
  122.  
  123. /* Get the cuid of an insn.  */
  124.  
  125. #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
  126.  
  127.  
  128. /* Record last point of death of (hard or pseudo) register n.  */
  129.  
  130. static rtx *reg_last_death;
  131.  
  132. /* Record last point of modification of (hard or pseudo) register n.  */
  133.  
  134. static rtx *reg_last_set;
  135.  
  136. /* Record the cuid of the last insn that invalidated memory
  137.    (anything that writes memory, and subroutine calls).  */
  138.  
  139. static int mem_last_set;
  140.  
  141. /* Record the cuid of the last CALL_INSN
  142.    so we can tell whether a potential combination crosses any calls.  */
  143.  
  144. static int last_call_cuid;
  145.  
  146. /* When `subst' is called, this is the insn that is being modified
  147.    (by combining in a previous insn).  The PATTERN of this insn
  148.    is still the old pattern partially modified and it should not be
  149.    looked at, but this may be used to examine the successors of the insn
  150.    to judge whether a simplification is valid.  */
  151.  
  152. static rtx subst_insn;
  153.  
  154. /* Record one modification to rtl structure
  155.    to be undone by storing old_contents into *where.
  156.    is_int is 1 if the contents are an int.  */
  157.  
  158. struct undo
  159. {
  160.   rtx *where;
  161.   rtx old_contents;
  162.   int is_int;
  163. };
  164.  
  165. struct undo_int
  166. {
  167.   int *where;
  168.   int old_contents;
  169.   int is_int;
  170. };
  171.  
  172. /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
  173.    num_undo says how many are currently recorded.
  174.    storage is nonzero if we must undo the allocation of new storage.
  175.    The value of storage is what to pass to obfree.  */
  176.  
  177. #define MAX_UNDO 10
  178.  
  179. struct undobuf
  180. {
  181.   int num_undo;
  182.   char *storage;
  183.   struct undo undo[MAX_UNDO];
  184. };
  185.  
  186. static struct undobuf undobuf;
  187.  
  188. /* Number of times the pseudo being substituted for
  189.    was found and replaced.  */
  190.  
  191. static int n_occurrences;
  192.  
  193. static void move_deaths ();
  194. static void move_deaths_2 ();
  195. void remove_death ();
  196. static void record_dead_and_set_regs ();
  197. int regno_dead_p ();
  198. static int use_crosses_set_p ();
  199. static int try_combine ();
  200. static rtx try_distrib ();
  201. static rtx subst ();
  202. static void undo_all ();
  203. static void copy_substitutions ();
  204. static void add_links ();
  205. static void remove_links ();
  206. static void add_incs ();
  207. static int adjacent_insns_p ();
  208. static int check_asm_operands ();
  209. static rtx simplify_and_const_int ();
  210. static rtx gen_lowpart_for_combine ();
  211. static void simplify_set_cc0_and ();
  212.  
  213. /* Main entry point for combiner.  F is the first insn of the function.
  214.    NREGS is the first unused pseudo-reg number.  */
  215.  
  216. void
  217. combine_instructions (f, nregs)
  218.      rtx f;
  219.      int nregs;
  220. {
  221.   register rtx insn;
  222.   register int i;
  223.   register rtx links, nextlinks;
  224.   rtx prev;
  225.  
  226.   combine_attempts = 0;
  227.   combine_merges = 0;
  228.   combine_extras = 0;
  229.   combine_successes = 0;
  230.   distrib_attempts = 0;
  231.   distrib_merges_1 = 0;
  232.   distrib_merges_2 = 0;
  233.   distrib_successes = 0;
  234.  
  235.   reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
  236.   reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
  237.   bzero (reg_last_death, nregs * sizeof (rtx));
  238.   bzero (reg_last_set, nregs * sizeof (rtx));
  239.  
  240.   init_recog ();
  241.  
  242.   /* Compute maximum uid value so uid_cuid can be allocated.  */
  243.  
  244.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  245.     if (INSN_UID (insn) > i)
  246.       i = INSN_UID (insn);
  247.  
  248.   uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
  249.  
  250.   /* Compute the mapping from uids to cuids.
  251.      Cuids are numbers assigned to insns, like uids,
  252.      except that cuids increase monotonically through the code.  */
  253.  
  254.   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
  255.     INSN_CUID (insn) = ++i;
  256.  
  257.   /* Now scan all the insns in forward order.  */
  258.  
  259.   last_call_cuid = 0;
  260.   mem_last_set = 0;
  261.   prev = 0;
  262.  
  263.   for (insn = f; insn; insn = NEXT_INSN (insn))
  264.     {
  265.       if (GET_CODE (insn) == INSN
  266.       || GET_CODE (insn) == CALL_INSN
  267.       || GET_CODE (insn) == JUMP_INSN)
  268.     {
  269.     retry:
  270.       /* Try this insn with each insn it links back to.  */
  271.  
  272.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  273.         if (try_combine (insn, XEXP (links, 0), 0))
  274.           goto retry;
  275.  
  276.       /* Try each sequence of three linked insns ending with this one.  */
  277.  
  278.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  279.         if (GET_CODE (XEXP (links, 0)) != NOTE)
  280.           for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
  281.            nextlinks = XEXP (nextlinks, 1))
  282.         if (try_combine (insn, XEXP (links, 0), XEXP (nextlinks, 0)))
  283.           goto retry;
  284.  
  285.       /* Try to combine a jump insn that uses CC0
  286.          with a preceding insn that sets CC0, and maybe with its
  287.          logical predecessor as well.
  288.          This is how we make decrement-and-branch insns.
  289.          We need this special code because data flow connections
  290.          via CC0 do not get entered in LOG_LINKS.  */
  291.  
  292.       if (GET_CODE (insn) == JUMP_INSN
  293.           && prev != 0
  294.           && GET_CODE (prev) == INSN
  295.           && GET_CODE (PATTERN (prev)) == SET
  296.           && GET_CODE (SET_DEST (PATTERN (prev))) == CC0)
  297.         {
  298.           if (try_combine (insn, prev, 0))
  299.         goto retry;
  300.  
  301.           if (GET_CODE (prev) != NOTE)
  302.         for (nextlinks = LOG_LINKS (prev); nextlinks;
  303.              nextlinks = XEXP (nextlinks, 1))
  304.           if (try_combine (insn, prev, XEXP (nextlinks, 0)))
  305.             goto retry;
  306.         }
  307.  
  308.       /* Try to apply the distributive law to this insn
  309.          and two insns that compute the operands of this one.  */
  310.       for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  311.         if (GET_CODE (XEXP (links, 0)) != NOTE)
  312.           for (nextlinks = XEXP (links, 1); nextlinks; nextlinks = XEXP (nextlinks, 1))
  313.         if (GET_CODE (XEXP (nextlinks, 0)) != NOTE)
  314.           {
  315.             rtx try_from = 0;
  316.  
  317.             if (GET_CODE (PATTERN (XEXP (links, 0))) == SET
  318.             && find_reg_note (insn, REG_DEAD, SET_DEST (PATTERN (XEXP (links, 0))))
  319.             && GET_CODE (PATTERN (XEXP (nextlinks, 0))) == SET
  320.             && find_reg_note (insn, REG_DEAD, SET_DEST (PATTERN (XEXP (nextlinks, 0)))))
  321.               try_from = try_distrib (insn, XEXP (links, 0), XEXP (nextlinks, 0));
  322.             if (try_from != 0)
  323.               {
  324.             insn = try_from;
  325.             goto retry;
  326.               }
  327.           }
  328. #if 0
  329. /* Turned off because on 68020 it takes four insns to make
  330.    something like (a[b / 32] & (1 << (31 - (b % 32)))) != 0
  331.    that could actually be optimized, and that's an unlikely piece of code.  */
  332.       /* If an insn gets or sets a bit field, try combining it
  333.          with two different insns whose results it uses.  */
  334.       if (GET_CODE (insn) == INSN
  335.           && GET_CODE (PATTERN (insn)) == SET
  336.           && (GET_CODE (SET_DEST (PATTERN (insn))) == ZERO_EXTRACT
  337.           || GET_CODE (SET_DEST (PATTERN (insn))) == SIGN_EXTRACT
  338.           || GET_CODE (SET_SRC (PATTERN (insn))) == ZERO_EXTRACT
  339.           || GET_CODE (SET_SRC (PATTERN (insn))) == SIGN_EXTRACT))
  340.         {
  341.           for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
  342.         if (GET_CODE (XEXP (links, 0)) != NOTE)
  343.           for (nextlinks = XEXP (links, 1); nextlinks;
  344.                nextlinks = XEXP (nextlinks, 1))
  345.             if (try_combine (insn, XEXP (links, 0), XEXP (nextlinks, 0)))
  346.               goto retry;
  347.         }
  348. #endif
  349.       if (GET_CODE (insn) != NOTE)
  350.         record_dead_and_set_regs (insn);
  351.       prev = insn;
  352.     }
  353.       else if (GET_CODE (insn) != NOTE)
  354.     prev = 0;
  355.     }
  356.   total_attempts += combine_attempts;
  357.   total_merges += combine_merges;
  358.   total_extras += combine_extras;
  359.   total_successes += combine_successes;
  360. }
  361.  
  362. /* Try to combine the insns I1 and I2 into I3.
  363.    Here I1 appears earlier than I2, which is earlier than I3.
  364.    I1 can be zero; then we combine just I2 into I3.
  365.  
  366.    Return 1 if successful; if that happens, I1 and I2 are pseudo-deleted
  367.    by turning them into NOTEs, and I3 is modified.
  368.    Return 0 if the combination does not work.  Then nothing is changed.  */
  369. #if defined( DSP56000 ) || defined( DSP96000 )
  370. /* Problem: The combiner will join register loads with arithmetic
  371.    insns. This keeps MACs from being detected, and makes extra work for the
  372.    reload pass. We can't just keep loads from combining, because 
  373.    this would turn off auto-increment and auto-decrement use. */
  374. #endif
  375.  
  376. static int
  377. try_combine (i3, i2, i1)
  378.      register rtx i3, i2, i1;
  379. {
  380.   register rtx newpat;
  381.   int added_sets_1 = 0;
  382.   int added_sets_2 = 0;
  383.   int total_sets;
  384.   int i2_is_used;
  385.   register rtx link;
  386.   int insn_code_number;
  387.   rtx i2dest, i2src;
  388.   rtx i1dest, i1src;
  389.   int maxreg;
  390.   rtx temp;
  391.   int i;
  392.  
  393.   combine_attempts++;
  394.  
  395.   /* Don't combine with something already used up by combination.  */
  396.  
  397.   if (GET_CODE (i2) == NOTE
  398.       || (i1 && GET_CODE (i1) == NOTE))
  399.     return 0;
  400.  
  401.   /* Don't combine across a CALL_INSN, because that would possibly
  402.      change whether the life span of some REGs crosses calls or not,
  403.      and it is a pain to update that information.  */
  404.  
  405.   if (INSN_CUID (i2) < last_call_cuid
  406.       || (i1 && INSN_CUID (i1) < last_call_cuid))
  407.     return 0;
  408.  
  409.   /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
  410.      That REG must be either set or dead by the final instruction
  411.      (so that we can safely forget about setting it).
  412.      Also test use_crosses_set_p to make sure that the value
  413.      that is to be substituted for the register
  414.      does not use any registers whose values alter in between.
  415.      Do not try combining with moves from one register to another
  416.      since it is better to let them be tied by register allocation.
  417.      (There is a switch to permit such combination; except the insns
  418.      that copy a function value into another register are never combined
  419.      because moving that too far away from the function call could cause
  420.      something else to be stored in that register in the interim.)
  421.  
  422.      A set of a SUBREG is considered as if it were a set from
  423.      SUBREG.  Thus, (SET (SUBREG:X (REG:Y...)) (something:X...))
  424.      is handled by substituting (SUBREG:Y (something:X...)) for (REG:Y...).  */
  425.  
  426.   if (GET_CODE (PATTERN (i2)) != SET)
  427.     return 0;
  428.   i2dest = SET_DEST (PATTERN (i2));
  429.   i2src = SET_SRC (PATTERN (i2));
  430.   if (GET_CODE (i2dest) == SUBREG)
  431.     {
  432.       i2dest = SUBREG_REG (i2dest);
  433.       i2src = gen_rtx (SUBREG, GET_MODE (i2dest), i2src, 0);
  434.     }
  435.   /* Don't eliminate a store in the stack pointer.  */
  436.   if (i2dest == stack_pointer_rtx)
  437.     return 0;
  438.   /* Don't install a subreg involving two modes not tieable.
  439.      It can worsen register allocation, and can even make invalid reload insns,
  440.      since the reg inside may need to be copied from in the outside mode,
  441.      and that may be invalid if it is an fp reg copied in integer mode.  */
  442.   if (GET_CODE (i2src) == SUBREG
  443.       && ! MODES_TIEABLE_P (GET_MODE (i2src), GET_MODE (SUBREG_REG (i2src))))
  444.     return 0;
  445.   if (GET_CODE (i2dest) != CC0
  446.       && (GET_CODE (i2dest) != REG
  447.       || (GET_CODE (i2src) == REG
  448.           /* Do allow the combination of y = x; x = y; (with x dead)
  449.          because the result will turn into nothing.  */
  450.           && !(GET_CODE (PATTERN (i3)) == SET
  451.            && i2src == SET_DEST (PATTERN (i3)))
  452.           && (!flag_combine_regs
  453.           /* Don't substitute a function value reg for any other.  */
  454.           || FUNCTION_VALUE_REGNO_P (REGNO (i2src))))
  455.       || GET_CODE (i2src) == CALL
  456.       /* Don't substitute into an incremented register.  */
  457.       || find_reg_note (i3, REG_INC, i2dest)
  458.       || use_crosses_set_p (i2src, INSN_CUID (i2))))
  459.     return 0;
  460.   if (GET_CODE (i2src) == ASM_OPERANDS && MEM_VOLATILE_P (i2src))
  461.     return 0;
  462.   /* Don't substitute for a register intended as a clobberable operand.  */
  463.   if (GET_CODE (PATTERN (i3)) == PARALLEL)
  464.     for (i = 0; i < XVECLEN (PATTERN (i3), 0); i++)
  465.       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
  466.       && XEXP (XVECEXP (PATTERN (i3), 0, i), 0) == i2dest)
  467.     return 0;
  468.  
  469.   if (i1 != 0)
  470.     {
  471.       if (GET_CODE (PATTERN (i1)) != SET)
  472.     return 0;
  473.       i1dest = SET_DEST (PATTERN (i1));
  474.       i1src = SET_SRC (PATTERN (i1));
  475.       if (GET_CODE (i1dest) == SUBREG)
  476.     {
  477.       i1dest = SUBREG_REG (i1dest);
  478.       i1src = gen_rtx (SUBREG, GET_MODE (i1dest), i1src, 0);
  479.     }
  480.       if (i1dest == stack_pointer_rtx)
  481.     return 0;
  482.       if (GET_CODE (i1src) == SUBREG
  483.       && ! MODES_TIEABLE_P (GET_MODE (i1src),
  484.                 GET_MODE (SUBREG_REG (i1src))))
  485.     return 0;
  486.       if (GET_CODE (i1dest) != CC0
  487.       && (GET_CODE (i1dest) != REG
  488.           || (GET_CODE (i1src) == REG
  489.           && (!flag_combine_regs
  490.               || FUNCTION_VALUE_REGNO_P (REGNO (i1src))))
  491.           || GET_CODE (i1src) == CALL
  492.           || find_reg_note (i3, REG_INC, i1dest)
  493.           || find_reg_note (i2, REG_INC, i1dest)
  494.           || use_crosses_set_p (i1src, INSN_CUID (i1))))
  495.     return 0;
  496.       if (GET_CODE (i1src) == ASM_OPERANDS && MEM_VOLATILE_P (i1src))
  497.     return 0;
  498.       /* Don't substitute for a register intended as a clobberable operand.  */
  499.       if (GET_CODE (PATTERN (i3)) == PARALLEL)
  500.     for (i = 0; i < XVECLEN (PATTERN (i3), 0); i++)
  501.       if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
  502.           && XEXP (XVECEXP (PATTERN (i3), 0, i), 0) == i1dest)
  503.         return 0;
  504.     }
  505.  
  506.   /* If it is better that two different modes keep two different pseudos,
  507.      avoid combining them.  */
  508.   if (GET_CODE (PATTERN (i3)) == SET)
  509.     {
  510.       rtx i3dest = SET_DEST (PATTERN (i3));
  511.       while (GET_CODE (i3dest) == SUBREG
  512.          || GET_CODE (i3dest) == STRICT_LOW_PART
  513.          || GET_CODE (i3dest) == SIGN_EXTRACT
  514.          || GET_CODE (i3dest) == ZERO_EXTRACT)
  515.     i3dest = SUBREG_REG (i3dest);
  516.  
  517.       if (SET_SRC (PATTERN (i3)) == i2dest
  518.       && GET_CODE (i3dest) == REG
  519.       && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (i3dest)))
  520.     return 0;
  521.     }
  522.  
  523.   /* If I2 contains anything volatile, reject, unless nothing
  524.      volatile comes between it and I3.  */
  525.   if (volatile_refs_p (PATTERN (i2)))
  526.     {
  527.       rtx insn;
  528.       for (insn = NEXT_INSN (i2); insn != i3; insn = NEXT_INSN (insn))
  529.     if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
  530.         || GET_CODE (insn) == JUMP_INSN)
  531.       if (volatile_refs_p (PATTERN (insn)))
  532.         return 0;
  533.     }
  534.   /* Likewise for I1; nothing volatile can come between it and I3,
  535.      except optionally I2.  */
  536.   if (i1 && volatile_refs_p (PATTERN (i1)))
  537.     {
  538.       rtx insn;
  539.       rtx end = (volatile_refs_p (PATTERN (i2)) ? i2 : i3);
  540.       for (insn = NEXT_INSN (i1); insn != end; insn = NEXT_INSN (insn))
  541.     if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
  542.         || GET_CODE (insn) == JUMP_INSN)
  543.       if (volatile_refs_p (PATTERN (insn)))
  544.         return 0;
  545.     }
  546.  
  547.   /* If I1 or I2 contains an autoincrement or autodecrement,
  548.      make sure that register is not used between there and I3,
  549.      and not already used in I3 either.
  550.      Also insist that I3 not be a jump; if it were one
  551.      and the incremented register were spilled, we would lose.  */
  552.   for (link = REG_NOTES (i2); link; link = XEXP (link, 1))
  553.     if (REG_NOTE_KIND (link) == REG_INC
  554.     && (GET_CODE (i3) == JUMP_INSN
  555.         || reg_used_between_p (XEXP (link, 0), i2, i3)
  556.         || reg_mentioned_p (XEXP (link, 0), PATTERN (i3))))
  557.       return 0;
  558.  
  559.   if (i1)
  560.     for (link = REG_NOTES (i1); link; link = XEXP (link, 1))
  561.       if (REG_NOTE_KIND (link) == REG_INC
  562.       && (GET_CODE (i3) == JUMP_INSN
  563.           || reg_used_between_p (XEXP (link, 0), i1, i3)
  564.           || reg_mentioned_p (XEXP (link, 0), PATTERN (i3))))
  565.     return 0;
  566.  
  567.   /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd,
  568.      EXCEPT in one case: I3 has a post-inc in an output operand.  */
  569.   if (!(GET_CODE (PATTERN (i3)) == SET
  570.     && GET_CODE (SET_SRC (PATTERN (i3))) == REG
  571.     && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
  572.     && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
  573.         || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
  574.     /* It's not the exception.  */
  575.     for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
  576.       if (REG_NOTE_KIND (link) == REG_INC
  577.       && (reg_mentioned_p (XEXP (link, 0), PATTERN (i2))
  578.           || (i1 != 0
  579.           && reg_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
  580.     return 0;
  581.  
  582.   /* Don't combine an insn I1 or I2 that follows a CC0-setting insn.
  583.      An insn that uses CC0 must not be separated from the one that sets it.
  584.      It would be more logical to test whether CC0 occurs inside I1 or I2,
  585.      but that would be much slower, and this ought to be equivalent.  */
  586.   temp = PREV_INSN (i2);
  587.   while (temp && GET_CODE (temp) == NOTE)
  588.     temp = PREV_INSN (temp);
  589.   if (temp && GET_CODE (temp) == INSN && sets_cc0_p (PATTERN (temp)))
  590.     return 0;
  591.   if (i1)
  592.     {
  593.       temp = PREV_INSN (i2);
  594.       while (temp && GET_CODE (temp) == NOTE)
  595.     temp = PREV_INSN (temp);
  596.       if (temp && GET_CODE (temp) == INSN && sets_cc0_p (PATTERN (temp)))
  597.     return 0;
  598.     }
  599.  
  600. #if defined( DSP56000 ) || defined( DSP96000 )
  601.   /* ok - here is where we screen for the merging of loads with 
  602.      arithmetic ops. First, we find the REG that is the object of the 
  603.      load. */
  604.   if (( i1 ) &&
  605.       ( INSN == GET_CODE ( i1 )) &&
  606.       ( SET == GET_CODE ( PATTERN ( i1 ))) &&
  607.       ( REG == GET_CODE ( SET_DEST ( PATTERN ( i1 )))) &&
  608.       ( MEM == GET_CODE ( SET_SRC ( PATTERN ( i1 )))) &&
  609.       (( reg_mentioned_p ( SET_DEST ( PATTERN ( i1 )), PATTERN ( i2 ))) ||
  610.        ( reg_mentioned_p ( SET_DEST ( PATTERN ( i1 )), PATTERN ( i3 )))))
  611.   {
  612.       return 0;
  613.   }
  614.   
  615.   if (( SET == GET_CODE ( PATTERN ( i2 ))) &&
  616.       ( REG == GET_CODE ( SET_DEST ( PATTERN ( i2 )))) &&
  617.       ( MEM == GET_CODE ( SET_SRC ( PATTERN ( i2 )))) &&
  618.       ( reg_mentioned_p ( SET_DEST ( PATTERN ( i2 )), PATTERN ( i3 ))))
  619.   {
  620.       return 0;
  621.   }
  622. #endif
  623.   /* See if the SETs in i1 or i2 need to be kept around in the merged
  624.      instruction: whenever the value set there is still needed past i3.  */
  625.   added_sets_2 = (GET_CODE (i2dest) != CC0
  626.           && ! dead_or_set_p (i3, i2dest));
  627.   if (i1)
  628.     added_sets_1 = ! (dead_or_set_p (i3, i1dest)
  629.               || dead_or_set_p (i2, i1dest));
  630.  
  631.   combine_merges++;
  632.  
  633.   undobuf.num_undo = 0;
  634.   undobuf.storage = 0;
  635.  
  636.   /* Substitute in the latest insn for the regs set by the earlier ones.  */
  637.  
  638.   maxreg = max_reg_num ();
  639.  
  640.   subst_insn = i3;
  641.   n_occurrences = 0;        /* `subst' counts here */
  642.  
  643.   newpat = subst (PATTERN (i3), i2dest, i2src);
  644.   /* Record whether i2's body now appears within i3's body.  */
  645.   i2_is_used = n_occurrences;
  646.  
  647.   if (i1)
  648.     {
  649.       n_occurrences = 0;
  650.       newpat = subst (newpat, i1dest, i1src);
  651.     }
  652.  
  653.   if (GET_CODE (PATTERN (i3)) == SET
  654.       && SET_DEST (PATTERN (i3)) == cc0_rtx
  655.       && (GET_CODE (SET_SRC (PATTERN (i3))) == AND
  656.       || GET_CODE (SET_SRC (PATTERN (i3))) == LSHIFTRT)
  657.       && next_insn_tests_no_inequality (i3))
  658.     simplify_set_cc0_and (i3);
  659.  
  660.   if (max_reg_num () != maxreg)
  661.     abort ();
  662.  
  663.   /* If the actions of the earler insns must be kept
  664.      in addition to substituting them into the latest one,
  665.      we must make a new PARALLEL for the latest insn
  666.      to hold additional the SETs.  */
  667.  
  668.   if (added_sets_1 || added_sets_2)
  669.     {
  670.       combine_extras++;
  671.  
  672.       /* Arrange to free later what we allocate now
  673.      if we don't accept this combination.  */
  674.       if (!undobuf.storage)
  675.     undobuf.storage = (char *) oballoc (0);
  676.  
  677.       if (GET_CODE (newpat) == PARALLEL)
  678.     {
  679.       rtvec old = XVEC (newpat, 0);
  680.       total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
  681.       newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
  682.       bcopy (&old->elem[0], &XVECEXP (newpat, 0, 0),
  683.          sizeof (old->elem[0]) * old->num_elem);
  684.     }
  685.       else
  686.     {
  687.       rtx old = newpat;
  688.       total_sets = 1 + added_sets_1 + added_sets_2;
  689.       newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
  690.       XVECEXP (newpat, 0, 0) = old;
  691.     }
  692.      if (added_sets_1)
  693.     {
  694.       XVECEXP (newpat, 0, --total_sets) = PATTERN (i1);
  695.     }
  696.      if (added_sets_2)
  697.     {
  698.       /* If there is no I1, use I2's body as is.  */
  699.       if (i1 == 0
  700.       /* If I2 was stuck into I3, then anything within it has
  701.          already had I1 substituted into it when that was done to I3.  */
  702.           || i2_is_used)
  703.         {
  704.           XVECEXP (newpat, 0, --total_sets) = PATTERN (i2);
  705.         }
  706.       else
  707.         XVECEXP (newpat, 0, --total_sets)
  708.           = subst (PATTERN (i2), i1dest, i1src);
  709.     }
  710.     }
  711.  
  712.   /* Fail if an autoincrement side-effect has been duplicated.  */
  713.   if ((i2_is_used > 1 && find_reg_note (i2, REG_INC, 0) != 0)
  714.       || (i1 != 0 && n_occurrences > 1 && find_reg_note (i1, REG_INC, 0) != 0))
  715.     {
  716.       undo_all ();
  717.       return 0;
  718.     }
  719.  
  720.   /* Is the result of combination a valid instruction?  */
  721.   insn_code_number = recog (newpat, i3);
  722.  
  723.   if (insn_code_number >= 0
  724.       /* Is the result a reasonable ASM_OPERANDS?  */
  725.       || (check_asm_operands (newpat) && ! added_sets_1 && ! added_sets_2))
  726.     {
  727.       /* Yes.  Install it.  */
  728.       register int regno;
  729.       INSN_CODE (i3) = insn_code_number;
  730.       PATTERN (i3) = newpat;
  731.       /* If anything was substituted more than once,
  732.      copy it to avoid invalid shared rtl structure.  */
  733.       copy_substitutions ();
  734.       /* The data flowing into I2 now flows into I3.
  735.      But we cannot always move all of I2's LOG_LINKS into I3,
  736.      since they must go to a setting of a REG from the
  737.      first use following.  If I2 was the first use following a set,
  738.      I3 is now a use, but it is not the first use
  739.      if some instruction between I2 and I3 is also a use.
  740.      Here, for simplicity, we move all the links only if
  741.      there are no real insns between I2 and I3.
  742.      Otherwise, we move only links that correspond to regs
  743.      that used to die in I2.  They are always safe to move.  */
  744.       add_links (i3, i2, adjacent_insns_p (i2, i3));
  745.       /* Most REGs that previously died in I2 now die in I3.  */ 
  746.       move_deaths (i2src, INSN_CUID (i2), i3);
  747.       if (GET_CODE (i2dest) == REG)
  748.     {
  749.       /* If the reg formerly set in I2 died only once and that was in I3,
  750.          zero its use count so it won't make `reload' do any work.  */
  751.       regno = REGNO (i2dest);
  752.       if (! added_sets_2)
  753.         {
  754.           reg_n_sets[regno]--;
  755.           /* Used to check  && regno_dead_p (regno, i3)  also here.  */
  756.           if (reg_n_sets[regno] == 0
  757.           && ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
  758.             & (1 << (regno % HOST_BITS_PER_INT))))
  759.         reg_n_refs[regno] = 0;
  760.         }
  761.       /* If a ref to REGNO was substituted into I3 from I2,
  762.          then it still dies there if it previously did.
  763.          Otherwise either REGNO never did die in I3 so remove_death is safe
  764.          or this entire life of REGNO is gone so remove its death.  */
  765.       if (!added_sets_2
  766.           && ! reg_mentioned_p (i2dest, PATTERN (i3)))
  767.         remove_death (regno, i3);
  768.     }
  769.       /* Any registers previously autoincremented in I2
  770.      are now incremented in I3.  */
  771.       add_incs (i3, REG_NOTES (i2));
  772.       if (i1)
  773.     {
  774.       /* Likewise, merge the info from I1 and get rid of it.  */
  775.       add_links (i3, i1,
  776.              adjacent_insns_p (i1, i2) && adjacent_insns_p (i2, i3));
  777.       move_deaths (i1src, INSN_CUID (i1), i3);
  778.       if (GET_CODE (i1dest) == REG)
  779.         {
  780.           regno = REGNO (i1dest);
  781.           if (! added_sets_1)
  782.         {
  783.           reg_n_sets[regno]--;
  784.           /* Used to also check  && regno_dead_p (regno, i3) here.  */
  785.  
  786.           if (reg_n_sets[regno] == 0
  787.               && ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
  788.                 & (1 << (regno % HOST_BITS_PER_INT))))
  789.  
  790.             reg_n_refs[regno] = 0;
  791.         }
  792.           /* If a ref to REGNO was substituted into I3 from I1,
  793.          then it still dies there if it previously did.
  794.          Else either REGNO never did die in I3 so remove_death is safe
  795.          or this entire life of REGNO is gone so remove its death.  */
  796.           if (! added_sets_1
  797.           && ! reg_mentioned_p (i1dest, PATTERN (i3)))
  798.         remove_death (regno, i3);
  799.         }
  800.       add_incs (i3, REG_NOTES (i1));
  801.       LOG_LINKS (i1) = 0;
  802.       PUT_CODE (i1, NOTE);
  803.       NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
  804.       NOTE_SOURCE_FILE (i1) = 0;
  805.     }
  806.       /* Get rid of I2.  */
  807.       LOG_LINKS (i2) = 0;
  808.       PUT_CODE (i2, NOTE);
  809.       NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
  810.       NOTE_SOURCE_FILE (i2) = 0;
  811.  
  812.       combine_successes++;
  813.       return 1;
  814.     }
  815.  
  816.   /* Failure: change I3 back the way it was.  */
  817.   undo_all ();
  818.  
  819.   return 0;
  820. }
  821.  
  822. /* Undo all the modifications recorded in undobuf.  */
  823.  
  824. static void
  825. undo_all ()
  826. {
  827.   register int i;
  828.   if (undobuf.num_undo > MAX_UNDO)
  829.     undobuf.num_undo = MAX_UNDO;
  830.   for (i = undobuf.num_undo - 1; i >= 0; i--)
  831.     *undobuf.undo[i].where = undobuf.undo[i].old_contents;
  832.   if (undobuf.storage)
  833.     obfree (undobuf.storage);
  834.   undobuf.num_undo = 0;
  835.   undobuf.storage = 0;
  836. }
  837.  
  838. /* If this insn had more than one substitution,
  839.    copy all but one, so that no invalid shared substructure is introduced.  */
  840.  
  841. static void
  842. copy_substitutions ()
  843. {
  844.   register int i;
  845.   if (undobuf.num_undo > 1)
  846.     {
  847.       for (i = undobuf.num_undo - 1; i >= 1; i--)
  848.     if (! undobuf.undo[i].is_int)
  849.       *undobuf.undo[i].where = copy_rtx (*undobuf.undo[i].where);
  850.     }
  851. }
  852.  
  853. /* Throughout X, replace FROM with TO, and return the result.
  854.    The result is TO if X is FROM;
  855.    otherwise the result is X, but its contents may have been modified.
  856.    If they were modified, a record was made in undobuf so that
  857.    undo_all will (among other things) return X to its original state.
  858.  
  859.    If the number of changes necessary is too much to record to undo,
  860.    the excess changes are not made, so the result is invalid.
  861.    The changes already made can still be undone.
  862.    undobuf.num_undo is incremented for such changes, so by testing that
  863.    the caller can tell whether the result is valid.
  864.  
  865.    `n_occurrences' is incremented each time FROM is replaced.  */
  866.  
  867. static rtx
  868. subst (x, from, to)
  869.      register rtx x, from, to;
  870. {
  871.   register char *fmt;
  872.   register int len, i;
  873.   register enum rtx_code code;
  874.   char was_replaced[2];
  875.  
  876. #define SUBST(INTO, NEWVAL)  \
  877.  do { if (undobuf.num_undo < MAX_UNDO)                    \
  878.     {                                \
  879.       undobuf.undo[undobuf.num_undo].where = &INTO;            \
  880.       undobuf.undo[undobuf.num_undo].old_contents = INTO;        \
  881.       undobuf.undo[undobuf.num_undo].is_int = 0;            \
  882.       INTO = NEWVAL;                        \
  883.     }                                \
  884.       undobuf.num_undo++; } while (0)
  885.  
  886. #define SUBST_INT(INTO, NEWVAL)  \
  887.  do { if (undobuf.num_undo < MAX_UNDO)                    \
  888.     {                                \
  889.       struct undo_int *u = (struct undo_int *)&undobuf.undo[undobuf.num_undo];\
  890.       u->where = &INTO;                        \
  891.       u->old_contents = INTO;                    \
  892.       u->is_int = 1;                        \
  893.       INTO = NEWVAL;                        \
  894.     }                                \
  895.       undobuf.num_undo++; } while (0)
  896.  
  897. /* FAKE_EXTEND_SAFE_P (MODE, FROM) is 1 if (subreg:MODE FROM 0) is a safe
  898.    replacement for (zero_extend:MODE FROM) or (sign_extend:MODE FROM).
  899.    If it is 0, that cannot be done.  We can now do this for any MEM
  900.    because (SUBREG (MEM...)) is guaranteed to cause the MEM to be reloaded.
  901.    If not for that, MEM's would very rarely be safe.  */
  902.  
  903. /* Reject MODEs bigger than a word, because we might not be able
  904.    to reference a two-register group starting with an arbitrary register
  905.    (and currently gen_lowpart might crash for a SUBREG).  */
  906.  
  907. #define FAKE_EXTEND_SAFE_P(MODE, FROM) \
  908.   (GET_MODE_SIZE (MODE) <= UNITS_PER_WORD            \
  909.    && (GET_CODE (FROM) == REG || GET_CODE (FROM) == SUBREG    \
  910.        || GET_CODE (FROM) == MEM))
  911.  
  912.   if (x == from)
  913.     return to;
  914.  
  915.   /* It is possible to have a subexpression appear twice in the insn.
  916.      Suppose that FROM is a register that appears within TO.
  917.      Then, after that subexpression has been scanned once by `subst',
  918.      the second time it is scanned, TO may be found.  If we were
  919.      to scan TO here, we would find FROM within it and create a
  920.      self-referent rtl structure which is completely wrong.  */
  921.   if (x == to)
  922.     return to;
  923.  
  924.   code = GET_CODE (x);
  925.  
  926.   /* A little bit of algebraic simplification here.  */
  927.   switch (code)
  928.     {
  929.       /* This case has no effect except to speed things up.  */
  930.     case REG:
  931.     case CONST_INT:
  932.     case CONST:
  933.     case SYMBOL_REF:
  934.     case LABEL_REF:
  935.     case PC:
  936.     case CC0:
  937.       return x;
  938.     }
  939.  
  940.   was_replaced[0] = 0;
  941.   was_replaced[1] = 0;
  942.  
  943.   len = GET_RTX_LENGTH (code);
  944.   fmt = GET_RTX_FORMAT (code);
  945.  
  946.   /* Don't replace FROM where it is being stored in rather than used.  */
  947.   if (code == SET && SET_DEST (x) == from)
  948.     fmt = "ie";
  949.   if (code == SET && GET_CODE (SET_DEST (x)) == SUBREG
  950.       && SUBREG_REG (SET_DEST (x)) == from)
  951.     fmt = "ie";
  952.  
  953.   for (i = 0; i < len; i++)
  954.     {
  955.       if (fmt[i] == 'E')
  956.     {
  957.       register int j;
  958.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  959.         {
  960.           register rtx new;
  961.           if (XVECEXP (x, i, j) == from)
  962.         new = to, n_occurrences++;
  963.           else
  964.         new = subst (XVECEXP (x, i, j), from, to);
  965.           if (new != XVECEXP (x, i, j))
  966.         SUBST (XVECEXP (x, i, j), new);
  967.         }
  968.     }
  969.       else if (fmt[i] == 'e')
  970.     {
  971.       register rtx new;
  972.  
  973.       if (XEXP (x, i) == from)
  974.         {
  975.           new = to;
  976.           n_occurrences++;
  977.           if (i < 2)
  978.         was_replaced[i] = 1;
  979.         }
  980.       else
  981.         new = subst (XEXP (x, i), from, to);
  982.  
  983.       if (new != XEXP (x, i))
  984.         SUBST (XEXP (x, i), new);
  985.     }
  986.     }
  987.  
  988.   /* A little bit of algebraic simplification here.  */
  989.   switch (code)
  990.     {
  991.     case SUBREG:
  992.       /* Changing mode twice with SUBREG => just change it once,
  993.      or not at all if changing back to starting mode.  */
  994.       if (SUBREG_REG (x) == to
  995.       && GET_CODE (to) == SUBREG)
  996.     {
  997.       if (GET_MODE (x) == GET_MODE (SUBREG_REG (to)))
  998.         if (SUBREG_WORD (x) == 0 && SUBREG_WORD (to) == 0)
  999.           return SUBREG_REG (to);
  1000.       SUBST (SUBREG_REG (x), SUBREG_REG (to));
  1001.       if (SUBREG_WORD (to) != 0)
  1002.         SUBST_INT (SUBREG_WORD (x), SUBREG_WORD (x) + SUBREG_WORD (to));
  1003.     }
  1004.       if (SUBREG_REG (x) == to
  1005.       && (GET_CODE (to) == SIGN_EXTEND || GET_CODE (to) == ZERO_EXTEND)
  1006.       && subreg_lowpart_p (x))
  1007.     {
  1008.       /* (subreg (sign_extend X)) is X, if it has same mode as X.  */
  1009.       if (GET_MODE (x) == GET_MODE (XEXP (to, 0)))
  1010.         return XEXP (to, 0);
  1011.       /* (subreg (sign_extend X)), if it has a mode wider than X,
  1012.          can be done with (sign_extend X).  */
  1013.       if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (XEXP (to, 0))))
  1014.         {
  1015.           if (!undobuf.storage)
  1016.         undobuf.storage = (char *) oballoc (0);
  1017.           return gen_rtx (GET_CODE (to), GET_MODE (x), XEXP (to, 0));
  1018.         }
  1019.       /* Extend and then truncate smaller than it was to start with:
  1020.          no need to extend.  */
  1021.       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (GET_MODE (XEXP (to, 0))))
  1022.         {
  1023.           SUBST (XEXP (x, 0), XEXP (to, 0));
  1024.         }
  1025.     }
  1026.       /* (subreg:A (mem:B X) N) becomes a modified MEM.
  1027.      If we can't do that safely, then it becomes something nonsensical
  1028.      so that this combination won't take place.
  1029.      This avoids producing any (subreg (mem))s except in the special
  1030.      paradoxical case where gen_lowpart_for_combine makes them.  */
  1031.       if (SUBREG_REG (x) == to
  1032.       && GET_CODE (to) == MEM)
  1033.     {
  1034.       int endian_offset = 0;
  1035.       /* Don't combine this if mode A is wider than B.  */
  1036.       if (GET_MODE_SIZE (GET_MODE (x)) > GET_MODE_SIZE (GET_MODE (to)))
  1037.         return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1038.       /* Don't change the mode of the MEM
  1039.          if that would change the meaning of the address.  */
  1040.       if (mode_dependent_address_p (XEXP (to, 0)))
  1041.         return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1042. #ifdef BYTES_BIG_ENDIAN
  1043.       if (GET_MODE_SIZE (GET_MODE (x)) < UNITS_PER_WORD)
  1044.         endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (GET_MODE (x));
  1045.       if (GET_MODE_SIZE (GET_MODE (to)) < UNITS_PER_WORD)
  1046.         endian_offset -= UNITS_PER_WORD - GET_MODE_SIZE (GET_MODE (to));
  1047. #endif
  1048.       if (!undobuf.storage)
  1049.         undobuf.storage = (char *) oballoc (0);
  1050.       /* Note if the plus_constant doesn't make a valid address
  1051.          then this combination won't be accepted.  */
  1052.       return gen_rtx (MEM, GET_MODE (x),
  1053.               plus_constant (XEXP (to, 0),
  1054.                      (SUBREG_WORD (x) * UNITS_PER_WORD
  1055.                       + endian_offset)));
  1056.     }
  1057.       break;
  1058.  
  1059.     case NOT:
  1060.       /* (not (minus X 1)) can become (neg X).  */
  1061.       if (was_replaced[0]
  1062.       && ((GET_CODE (to) == PLUS && INTVAL (XEXP (to, 1)) == -1)
  1063.           || (GET_CODE (to) == MINUS && XEXP (to, 1) == const1_rtx)))
  1064.     {
  1065.       if (!undobuf.storage)
  1066.         undobuf.storage = (char *) oballoc (0);
  1067.       return gen_rtx (NEG, GET_MODE (to), XEXP (to, 0));
  1068.     }
  1069.       /* Don't let substitution introduce double-negatives.  */
  1070.       if (was_replaced[0]
  1071.       && GET_CODE (to) == code)
  1072.     return XEXP (to, 0);
  1073.       break;
  1074.  
  1075.     case NEG:
  1076.       /* (neg (minus X Y)) can become (minus Y X).  */
  1077.       if (was_replaced[0] && GET_CODE (to) == MINUS)
  1078.     {
  1079.       if (!undobuf.storage)
  1080.         undobuf.storage = (char *) oballoc (0);
  1081.       return gen_rtx (MINUS, GET_MODE (to),
  1082.               XEXP (to, 1), XEXP (to, 0));
  1083.     }
  1084.       /* Don't let substitution introduce double-negatives.  */
  1085.       if (was_replaced[0]
  1086.       && GET_CODE (to) == code)
  1087.     return XEXP (to, 0);
  1088.       break;
  1089.  
  1090.     case FLOAT_TRUNCATE:
  1091.       /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF.  */
  1092.       if (was_replaced[0]
  1093.       && GET_CODE (to) == FLOAT_EXTEND
  1094.       && GET_MODE (XEXP (to, 0)) == GET_MODE (x))
  1095.     return XEXP (to, 0);
  1096.       break;
  1097.  
  1098. #if 0
  1099.     case COMPARE:
  1100.       /* -x>0 if 0>x.  */
  1101.       if (GET_CODE (XEXP (x, 0)) == NEG && XEXP (x, 1) == const0_rtx)
  1102.     {
  1103.       SUBST (XEXP (x, 1), XEXP (XEXP (x, 0), 0));
  1104.       SUBST (XEXP (x, 0), const0_rtx);
  1105.     }
  1106.       if (GET_CODE (XEXP (x, 1)) == NEG && XEXP (x, 0) == const0_rtx)
  1107.     {
  1108.       SUBST (XEXP (x, 0), XEXP (XEXP (x, 1), 0));
  1109.       SUBST (XEXP (x, 1), const0_rtx);
  1110.     }
  1111.       break;
  1112. #endif
  1113.  
  1114.     case PLUS:
  1115. #if 0  /* Turned off for caution: turn it on after 1.36.  */
  1116.       /* Identify constant sums as such.  */
  1117.       if ((was_replaced[0] || was_replaced[1])
  1118.       && CONSTANT_P (XEXP (x, 0))
  1119.       && CONSTANT_P (XEXP (x, 1)))
  1120.     {
  1121.       if (!undobuf.storage)
  1122.         undobuf.storage = (char *) oballoc (0);
  1123.       return gen_rtx (CONST, GET_MODE (x), x);
  1124.     }
  1125. #endif
  1126.       /* In (plus <foo> (ashift <bar> <n>))
  1127.      change the shift to a multiply so we can recognize
  1128.      scaled indexed addresses.  */
  1129.       if ((was_replaced[0]
  1130.        || was_replaced[1])
  1131.       && GET_CODE (to) == ASHIFT
  1132.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1133.       && INTVAL (XEXP (to, 1)) < HOST_BITS_PER_INT)
  1134.     {
  1135.       rtx temp;
  1136.       if (!undobuf.storage)
  1137.         undobuf.storage = (char *) oballoc (0);
  1138.       temp = gen_rtx (MULT, GET_MODE (to),
  1139.               XEXP (to, 0),
  1140.               gen_rtx (CONST_INT, VOIDmode,
  1141.                    1 << INTVAL (XEXP (to, 1))));
  1142.       if (was_replaced[0])
  1143.         SUBST (XEXP (x, 0), temp);
  1144.       else
  1145.         SUBST (XEXP (x, 1), temp);
  1146.     }
  1147.       /* (plus X (neg Y)) becomes (minus X Y).  */
  1148.       if (GET_CODE (XEXP (x, 1)) == NEG)
  1149.     {
  1150.       if (!undobuf.storage)
  1151.         undobuf.storage = (char *) oballoc (0);
  1152.       return gen_rtx (MINUS, GET_MODE (x),
  1153.               XEXP (x, 0), XEXP (XEXP (x, 1), 0));
  1154.     }
  1155.       /* (plus (neg X) Y) becomes (minus Y X).  */
  1156.       if (GET_CODE (XEXP (x, 0)) == NEG)
  1157.     {
  1158.       if (!undobuf.storage)
  1159.         undobuf.storage = (char *) oballoc (0);
  1160.       return gen_rtx (MINUS, GET_MODE (x),
  1161.               XEXP (x, 1), XEXP (XEXP (x, 0), 0));
  1162.     }
  1163.       /* (plus (plus x c1) c2) => (plus x c1+c2) */
  1164.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  1165.       && GET_CODE (XEXP (x, 0)) == PLUS
  1166.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
  1167.     {
  1168.       int sum = (INTVAL (XEXP (x, 1))
  1169.              + INTVAL (XEXP (XEXP (x, 0), 1)));
  1170.       if (sum == 0)
  1171.         return XEXP (XEXP (x, 0), 0);
  1172.       if (!undobuf.storage)
  1173.         undobuf.storage = (char *) oballoc (0);
  1174.       SUBST (XEXP (x, 1), gen_rtx (CONST_INT, VOIDmode, sum));
  1175.       SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  1176.       break;
  1177.     }
  1178.       /* If we have something (putative index) being added to a sum,
  1179.      associate it so that any constant term is outermost.
  1180.      That's because that's the way indexed addresses are
  1181.      now supposed to appear.  */
  1182.       if (((was_replaced[0] && GET_CODE (XEXP (x, 1)) == PLUS)
  1183.        || (was_replaced[1] && GET_CODE (XEXP (x, 0)) == PLUS))
  1184.       ||
  1185.       ((was_replaced[0] || was_replaced[1])
  1186.        && GET_CODE (to) == PLUS))
  1187.     {
  1188.       rtx offset = 0, base, index;
  1189.       if (GET_CODE (to) != PLUS)
  1190.         {
  1191.           index = to;
  1192.           base = was_replaced[0] ? XEXP (x, 1) : XEXP (x, 0);
  1193.         }
  1194.       else
  1195.         {
  1196.           index = was_replaced[0] ? XEXP (x, 1) : XEXP (x, 0);
  1197.           base = to;
  1198.         }
  1199.       if (CONSTANT_ADDRESS_P (XEXP (base, 0)))
  1200.         {
  1201.           offset = XEXP (base, 0);
  1202.           base = XEXP (base, 1);
  1203.         }
  1204.       else if (CONSTANT_ADDRESS_P (XEXP (base, 1)))
  1205.         {
  1206.           offset = XEXP (base, 1);
  1207.           base = XEXP (base, 0);
  1208.         }
  1209.       if (offset != 0)
  1210.         {
  1211.           if (!undobuf.storage)
  1212.         undobuf.storage = (char *) oballoc (0);
  1213.           if (GET_CODE (offset) == CONST_INT)
  1214.         return plus_constant (gen_rtx (PLUS, GET_MODE (index),
  1215.                            base, index),
  1216.                       INTVAL (offset));
  1217.           if (GET_CODE (index) == CONST_INT)
  1218.         return plus_constant (gen_rtx (PLUS, GET_MODE (offset),
  1219.                            base, offset),
  1220.                       INTVAL (index));
  1221.           return gen_rtx (PLUS, GET_MODE (index),
  1222.                   gen_rtx (PLUS, GET_MODE (index),
  1223.                        base, index),
  1224.                   offset);
  1225.         }
  1226.     }
  1227.       break;
  1228.  
  1229.     case EQ:
  1230.     case NE:
  1231.       /* If comparing a subreg against zero, discard the subreg.  */
  1232.       if (was_replaced[0]
  1233.       && GET_CODE (to) == SUBREG
  1234.       && SUBREG_WORD (to) == 0
  1235.       && XEXP (x, 1) == const0_rtx)
  1236.     SUBST (XEXP (x, 0), SUBREG_REG (to));
  1237.  
  1238.       /* If comparing a ZERO_EXTRACT against zero,
  1239.      canonicalize to a SIGN_EXTRACT,
  1240.      since the two are equivalent here.  */
  1241.       if (was_replaced[0]
  1242.       && GET_CODE (to) == ZERO_EXTRACT
  1243.       && XEXP (x, 1) == const0_rtx)
  1244.     {
  1245.       if (!undobuf.storage)
  1246.         undobuf.storage = (char *) oballoc (0);
  1247.       SUBST (XEXP (x, 0),
  1248.          gen_rtx (SIGN_EXTRACT, GET_MODE (to),
  1249.               XEXP (to, 0), XEXP (to, 1),
  1250.               XEXP (to, 2)));
  1251.     }
  1252.       /* If we are putting (ASHIFT 1 x) into (EQ (AND ... y) 0),
  1253.      arrange to return (EQ (SIGN_EXTRACT y 1 x) 0),
  1254.      which is what jump-on-bit instructions are written with.  */
  1255.       else if (XEXP (x, 1) == const0_rtx
  1256.            && GET_CODE (XEXP (x, 0)) == AND
  1257.            && (XEXP (XEXP (x, 0), 0) == to
  1258.            || XEXP (XEXP (x, 0), 1) == to)
  1259.            && GET_CODE (to) == ASHIFT
  1260.            && XEXP (to, 0) == const1_rtx)
  1261.     {
  1262.       register rtx y = XEXP (XEXP (x, 0),
  1263.                  XEXP (XEXP (x, 0), 0) == to);
  1264.       if (!undobuf.storage)
  1265.         undobuf.storage = (char *) oballoc (0);
  1266.       SUBST (XEXP (x, 0),
  1267.          gen_rtx (SIGN_EXTRACT, GET_MODE (to),
  1268.               y,
  1269.               const1_rtx, XEXP (to, 1)));
  1270.     }
  1271.       /* Negation is a no-op before equality test against zero.  */
  1272.       if (GET_CODE (XEXP (x, 0)) == NEG && XEXP (x, 1) == const0_rtx)
  1273.     {
  1274.       SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  1275.     }
  1276.       if (GET_CODE (XEXP (x, 1)) == NEG && XEXP (x, 0) == const0_rtx)
  1277.     {
  1278.       SUBST (XEXP (x, 1), XEXP (XEXP (x, 1), 0));
  1279.     }
  1280.       break;
  1281.  
  1282.     case ZERO_EXTEND:
  1283.       /* Nested zero-extends are equivalent to just one.  */
  1284.       if (was_replaced[0]
  1285.       && GET_CODE (to) == ZERO_EXTEND)
  1286.     SUBST (XEXP (x, 0), XEXP (to, 0));
  1287.       /* Zero extending a constant int can be replaced
  1288.      by a zero-extended constant.  */
  1289.       if (was_replaced[0]
  1290.       && HOST_BITS_PER_INT >= GET_MODE_BITSIZE (GET_MODE (from))
  1291.       && GET_CODE (to) == CONST_INT)
  1292.     {
  1293.       int intval = INTVAL (to) & GET_MODE_MASK (GET_MODE (from));
  1294.       if (!undobuf.storage)
  1295.         undobuf.storage = (char *) oballoc (0);
  1296.       return gen_rtx (CONST_INT, VOIDmode, intval);
  1297.     }
  1298.       /* Zero-extending the result of an and with a constant can be done
  1299.      with a wider and.  */
  1300.       if (was_replaced[0]
  1301.       && GET_CODE (to) == AND
  1302.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1303.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  1304.       /* Avoid getting wrong result if the constant has high bits set
  1305.          that are irrelevant in the narrow mode where it is being used.  */
  1306.       && 0 == (INTVAL (XEXP (to, 1))
  1307.            & ~ GET_MODE_MASK (GET_MODE (to))))
  1308.     {
  1309.       if (!undobuf.storage)
  1310.         undobuf.storage = (char *) oballoc (0);
  1311.       return gen_rtx (AND, GET_MODE (x),
  1312.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1313.               XEXP (to, 1));
  1314.     } 
  1315.       /* Change (zero_extend:M (subreg:N (zero_extract:M ...) 0))
  1316.      to (zero_extract:M ...) if the field extracted fits in mode N.  */
  1317.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  1318.       && GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTRACT
  1319.       && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
  1320.       && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
  1321.           <= GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
  1322.     {
  1323.       return XEXP (XEXP (x, 0), 0);
  1324.     }
  1325.       /* Change (zero_extend:M (subreg:N (and:M ... <const>) 0))
  1326.      to (and:M ...) if the significant bits fit in mode N.  */
  1327.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  1328.       && SUBREG_REG (XEXP (x, 0)) == to
  1329.       && SUBREG_WORD (XEXP (x, 0)) == 0
  1330.       && GET_CODE (to) == AND
  1331.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1332.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  1333.       /* Avoid getting wrong result if the constant has high bits set
  1334.          that are irrelevant in the narrow mode where it is being used.  */
  1335.       && 0 == (INTVAL (XEXP (to, 1))
  1336.            & ~ GET_MODE_MASK (GET_MODE (to))))
  1337.     {
  1338.       if (!undobuf.storage)
  1339.         undobuf.storage = (char *) oballoc (0);
  1340.       return gen_rtx (AND, GET_MODE (x),
  1341.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1342.               XEXP (to, 1));
  1343.     }
  1344.       /* In (zero_extend:M (subreg:N (lshiftrt:M REG))),
  1345.      where REG was assigned from (zero_extend:M (any:N ...)),
  1346.      remove the outer zero extension.  */
  1347.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  1348.       && SUBREG_REG (XEXP (x, 0)) == to
  1349.       && SUBREG_WORD (XEXP (x, 0)) == 0
  1350.       && GET_CODE (to) == LSHIFTRT)
  1351.     {
  1352.       rtx tmp = XEXP (to, 0);
  1353.  
  1354.       /* See if arg of LSHIFTRT is a register whose value we can find.  */
  1355.       if (GET_CODE (tmp) == REG)
  1356.         if (reg_n_sets[REGNO (tmp)] == 1
  1357.         && SET_DEST (PATTERN (reg_last_set[REGNO (tmp)])) == tmp)
  1358.           tmp = SET_SRC (PATTERN (reg_last_set[REGNO (tmp)]));
  1359.         else
  1360.           break;
  1361.  
  1362.       if (GET_CODE (tmp) == ZERO_EXTEND
  1363.           && GET_MODE (tmp) == GET_MODE (x)
  1364.           && GET_MODE (XEXP (tmp, 0)) == GET_MODE (XEXP (x, 0)))
  1365.         return SUBREG_REG (XEXP (x, 0));
  1366.     }
  1367.       break;
  1368.  
  1369.     case SIGN_EXTEND:
  1370.       /* Nested sign-extends are equivalent to just one.  */
  1371.       if (was_replaced[0]
  1372.       && GET_CODE (to) == SIGN_EXTEND)
  1373.     SUBST (XEXP (x, 0), XEXP (to, 0));
  1374.       /* Sign extending a constant int can be replaced
  1375.      by a sign-extended constant.  */
  1376.       if (was_replaced[0]
  1377.       && HOST_BITS_PER_INT >= GET_MODE_BITSIZE (GET_MODE (from))
  1378.       && GET_CODE (to) == CONST_INT)
  1379.     {
  1380.       int intval = INTVAL (to);
  1381.       if (!undobuf.storage)
  1382.         undobuf.storage = (char *) oballoc (0);
  1383.       if (intval > 0
  1384.           && (intval & (1 << (GET_MODE_BITSIZE (GET_MODE (from)) - 1))))
  1385.         intval |= ~ GET_MODE_MASK (GET_MODE (from));
  1386.       return gen_rtx (CONST_INT, VOIDmode, intval);
  1387.     }
  1388.       /* Sign-extending the result of an and with a constant can be done
  1389.      with a wider and, provided the high bit of the constant is 0.  */
  1390.       if (was_replaced[0]
  1391.       && GET_CODE (to) == AND
  1392.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1393.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  1394.       && ((INTVAL (XEXP (to, 1))
  1395.            & (-1 << (GET_MODE_BITSIZE (GET_MODE (to)) - 1)))
  1396.           == 0))
  1397.     {
  1398.       if (!undobuf.storage)
  1399.         undobuf.storage = (char *) oballoc (0);
  1400.       return gen_rtx (AND, GET_MODE (x),
  1401.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1402.               XEXP (to, 1));
  1403.      } 
  1404.       /* hacks added by tiemann.  */
  1405.       /* Change (sign_extend:M (subreg:N (and:M ... <const>) 0))
  1406.      to (and:M ...), provided the result fits in mode N,
  1407.      and the high bit of the constant is 0 in mode N.  */
  1408.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  1409.       && SUBREG_REG (XEXP (x, 0)) == to
  1410.       && SUBREG_WORD (XEXP (x, 0)) == 0
  1411.       && GET_CODE (to) == AND
  1412.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1413.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0))
  1414.       && ((INTVAL (XEXP (to, 1))
  1415.            & (-1 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
  1416.           == 0))
  1417.     {
  1418.       if (!undobuf.storage)
  1419.         undobuf.storage = (char *) oballoc (0);
  1420.       return gen_rtx (AND, GET_MODE (x),
  1421.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1422.               XEXP (to, 1));
  1423.     } 
  1424.       /* In (sign_extend:M (subreg:N (ashiftrt:M REG))),
  1425.      where REG was assigned from (sign_extend:M (any:N ...)),
  1426.      remove the outer sign extension.  */
  1427.       if (GET_CODE (XEXP (x, 0)) == SUBREG
  1428.       && SUBREG_REG (XEXP (x, 0)) == to
  1429.       && SUBREG_WORD (XEXP (x, 0)) == 0
  1430.       && GET_CODE (to) == ASHIFTRT)
  1431.     {
  1432.       rtx tmp = XEXP (to, 0);
  1433.  
  1434.       /* See if arg of LSHIFTRT is a register whose value we can find.  */
  1435.       if (GET_CODE (tmp) == REG)
  1436.         if (reg_n_sets[REGNO (tmp)] == 1
  1437.         && SET_DEST (PATTERN (reg_last_set[REGNO (tmp)])) == tmp)
  1438.           tmp = SET_SRC (PATTERN (reg_last_set[REGNO (tmp)]));
  1439.         else
  1440.           break;
  1441.  
  1442.       if (GET_CODE (tmp) == SIGN_EXTEND
  1443.           && GET_MODE (tmp) == GET_MODE (x)
  1444.           && GET_MODE (XEXP (tmp, 0)) == GET_MODE (XEXP (x, 0)))
  1445.         return SUBREG_REG (XEXP (x, 0));
  1446.     }
  1447.       break;
  1448.  
  1449.     case SET:
  1450.       /* In (set (zero-extract <x> <n> <y>) (and <foo> <(2**n-1) | anything>))
  1451.      the `and' can be deleted.  This can happen when storing a bit
  1452.      that came from a set-flag insn followed by masking to one bit.  */
  1453.       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  1454.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1455.       && was_replaced[1]
  1456.       && GET_CODE (to) == AND
  1457.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1458.       && 0 == (((1 << INTVAL (XEXP (XEXP (x, 0), 1))) - 1)
  1459.            & ~ INTVAL (XEXP (to, 1))))
  1460.     {
  1461.       SUBST (XEXP (x, 1), XEXP (to, 0));
  1462.     } 
  1463.       /* In (set (zero-extract <x> <n> <y>)
  1464.          (subreg (and <foo> <(2**n-1) | anything>)))
  1465.      the `and' can be deleted.  */
  1466.       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  1467.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1468.       && GET_CODE (XEXP (x, 1)) == SUBREG
  1469.       && SUBREG_WORD (XEXP (x, 1)) == 0
  1470.       && GET_CODE (SUBREG_REG (XEXP (x, 1))) == AND
  1471.       && GET_CODE (XEXP (SUBREG_REG (XEXP (x, 1)), 1)) == CONST_INT
  1472.       && 0 == (((1 << INTVAL (XEXP (XEXP (x, 0), 1))) - 1)
  1473.            & ~ INTVAL (XEXP (SUBREG_REG (XEXP (x, 1)), 1))))
  1474.     {
  1475.       SUBST (SUBREG_REG (XEXP (x, 1)), XEXP (SUBREG_REG (XEXP (x, 1)), 0));
  1476.     } 
  1477.       /* (set (zero_extract ...) (and/or/xor (zero_extract ...) const)),
  1478.      if both zero_extracts have the same location, size and position,
  1479.      can be changed to avoid the byte extracts.  */
  1480.       if ((GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
  1481.        || GET_CODE (XEXP (x, 0)) == SIGN_EXTRACT)
  1482.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1483.       && (GET_CODE (XEXP (x, 1)) == AND
  1484.           || GET_CODE (XEXP (x, 1)) == IOR
  1485.           || GET_CODE (XEXP (x, 1)) == XOR)
  1486.       && rtx_equal_p (XEXP (x, 0), XEXP (XEXP (x, 1), 0))
  1487.       && GET_CODE (XEXP (XEXP (x, 1), 0)) == GET_CODE (XEXP (x, 0))
  1488.       && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
  1489.       /* zero_extract can apply to a QImode even if the bits extracted
  1490.          don't fit inside that byte.  In such a case, we may not do this
  1491.          optimization, since the OR or AND insn really would need
  1492.          to fit in a byte.  */
  1493.       && (INTVAL (XEXP (XEXP (x, 0), 1)) + INTVAL (XEXP (XEXP (x, 0), 2))
  1494.           < GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))))
  1495.     {
  1496.       int shiftcount;
  1497.       int newmask;
  1498. #ifdef BITS_BIG_ENDIAN
  1499.       shiftcount
  1500.         = GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
  1501.           - INTVAL (XEXP (XEXP (x, 0), 1)) - INTVAL (XEXP (XEXP (x, 0), 2));
  1502. #else
  1503.       shiftcount
  1504.         = INTVAL (XEXP (XEXP (x, 0), 2));
  1505. #endif
  1506.       newmask = ((INTVAL (XEXP (XEXP (x, 1), 1)) << shiftcount)
  1507.              + (GET_CODE (XEXP (x, 1)) == AND
  1508.             ? (1 << shiftcount) - 1
  1509.             : 0));
  1510.       if (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
  1511.           < HOST_BITS_PER_INT)
  1512.         newmask &= (1 << GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))) - 1;
  1513.       if (!undobuf.storage)
  1514.         undobuf.storage = (char *) oballoc (0);
  1515.       return
  1516.         gen_rtx (SET, VOIDmode,
  1517.              XEXP (XEXP (x, 0), 0),
  1518.              gen_rtx (GET_CODE (XEXP (x, 1)),
  1519.                   GET_MODE (XEXP (XEXP (x, 0), 0)),
  1520.                   XEXP (XEXP (XEXP (x, 1), 0), 0),
  1521.                   gen_rtx (CONST_INT, VOIDmode, newmask)));
  1522.     }
  1523.       /* Can simplify (set (cc0) (compare (zero/sign_extend FOO) CONST))
  1524.      to (set (cc0) (compare FOO CONST)) if CONST fits in FOO's mode
  1525.      and we are only testing equality.
  1526.      In fact, this is valid for zero_extend if what follows is an
  1527.      unsigned comparison, and for sign_extend with a signed comparison.  */
  1528.       if (SET_DEST (x) == cc0_rtx
  1529.       && GET_CODE (SET_SRC (x)) == COMPARE
  1530.       && (GET_CODE (XEXP (SET_SRC (x), 0)) == ZERO_EXTEND
  1531.           || GET_CODE (XEXP (SET_SRC (x), 0)) == SIGN_EXTEND)
  1532.       && next_insn_tests_no_inequality (subst_insn)
  1533.       && GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
  1534.       /* This is overly cautious by one bit, but saves worrying about
  1535.          whether it is zero-extension or sign extension.  */
  1536.       && ((unsigned) INTVAL (XEXP (SET_SRC (x), 1))
  1537.           < (1 << (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (SET_SRC (x), 0), 0))) - 1))))
  1538.     SUBST (XEXP (SET_SRC (x), 0), XEXP (XEXP (SET_SRC (x), 0), 0));
  1539.       break;
  1540.  
  1541.     case AND:
  1542.       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
  1543.     {
  1544.       rtx tem = simplify_and_const_int (x, to);
  1545.       if (tem)
  1546.         return tem;
  1547.     }
  1548.       break;
  1549.  
  1550.     case IOR:
  1551.     case XOR:
  1552.       /* (ior (ior x c1) c2) => (ior x c1|c2); likewise for xor.  */
  1553.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  1554.       && GET_CODE (XEXP (x, 0)) == code
  1555.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
  1556.     {
  1557.       int c0 = INTVAL (XEXP (x, 1));
  1558.       int c1 = INTVAL (XEXP (XEXP (x, 0), 1));
  1559.       int combined = (code == IOR ? c0 | c1 : c0 ^ c1);
  1560.  
  1561.       if (combined == 0)
  1562.         return XEXP (XEXP (x, 0), 0);
  1563.       if (!undobuf.storage)
  1564.         undobuf.storage = (char *) oballoc (0);
  1565.       SUBST (XEXP (x, 1), gen_rtx (CONST_INT, VOIDmode, combined));
  1566.       SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
  1567.       break;
  1568.     }
  1569.  
  1570.     case FLOAT:
  1571.       /* (float (sign_extend <X>)) = (float <X>).  */
  1572.       if (was_replaced[0]
  1573.       && GET_CODE (to) == SIGN_EXTEND)
  1574.     SUBST (XEXP (x, 0), XEXP (to, 0));
  1575.       break;
  1576.  
  1577.     case ZERO_EXTRACT:
  1578.       /* (ZERO_EXTRACT (TRUNCATE x)...)
  1579.      can become (ZERO_EXTRACT x ...).  */
  1580.       if (was_replaced[0]
  1581.       && GET_CODE (to) == TRUNCATE)
  1582.     {
  1583. #ifdef BITS_BIG_ENDIAN
  1584.       if (GET_CODE (XEXP (x, 2)) == CONST_INT)
  1585.         {
  1586.           if (!undobuf.storage)
  1587.         undobuf.storage = (char *) oballoc (0);
  1588.           /* On a big-endian machine, must increment the bit-number
  1589.          since sign bit is farther away in the pre-truncated value.  */
  1590.           return gen_rtx (ZERO_EXTRACT, GET_MODE (x),
  1591.                   XEXP (to, 0),
  1592.                   XEXP (x, 1),
  1593.                   gen_rtx (CONST_INT, VOIDmode,
  1594.                        (INTVAL (XEXP (x, 2))
  1595.                     + GET_MODE_BITSIZE (GET_MODE (XEXP (to, 0)))
  1596.                     - GET_MODE_BITSIZE (GET_MODE (to)))));
  1597.         }
  1598. #else
  1599.       SUBST (XEXP (x, 0), XEXP (to, 0));
  1600. #endif
  1601.     }
  1602.       /* Extracting a single bit from the result of a shift:
  1603.      see which bit it was before the shift and extract that directly.  */
  1604.       if (was_replaced[0]
  1605.       && (GET_CODE (to) == ASHIFTRT || GET_CODE (to) == LSHIFTRT
  1606.           || GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1607.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1608.       && XEXP (x, 1) == const1_rtx
  1609.       && GET_CODE (XEXP (x, 2)) == CONST_INT)
  1610.     {
  1611.       int shift = INTVAL (XEXP (to, 1));
  1612.       int newpos;
  1613.       if (GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1614.         shift = - shift;
  1615. #ifdef BITS_BIG_ENDIAN
  1616.       shift = - shift;
  1617. #endif
  1618.       newpos = INTVAL (XEXP (x, 2)) + shift;
  1619.       if (newpos >= 0 &&
  1620.           newpos < GET_MODE_BITSIZE (GET_MODE (to)))
  1621.         {
  1622.           if (!undobuf.storage)
  1623.         undobuf.storage = (char *) oballoc (0);
  1624.           return gen_rtx (ZERO_EXTRACT, GET_MODE (x),
  1625.                   XEXP (to, 0), const1_rtx,
  1626.                   gen_rtx (CONST_INT, VOIDmode, newpos));
  1627.         }
  1628.     }
  1629.       break;
  1630.  
  1631.     case LSHIFTRT:
  1632.     case ASHIFTRT:
  1633.     case ROTATE:
  1634.     case ROTATERT:
  1635. #ifdef SHIFT_COUNT_TRUNCATED
  1636.       /* (lshift <X> (sign_extend <Y>)) = (lshift <X> <Y>) (most machines).
  1637.      True for all kinds of shifts and also for zero_extend.  */
  1638.       if (was_replaced[1]
  1639.       && (GET_CODE (to) == SIGN_EXTEND
  1640.           || GET_CODE (to) == ZERO_EXTEND)
  1641.       && FAKE_EXTEND_SAFE_P (GET_MODE (to), XEXP (to, 0)))
  1642.     {
  1643.       if (!undobuf.storage)
  1644.         undobuf.storage = (char *) oballoc (0);
  1645.       SUBST (XEXP (x, 1),
  1646.          /* This is a perverse SUBREG, wider than its base.  */
  1647.          gen_lowpart_for_combine (GET_MODE (to), XEXP (to, 0)));
  1648.     }
  1649. #endif
  1650.       /* Two shifts in a row of same kind
  1651.      in same direction with constant counts
  1652.      may be combined.  */
  1653.       if (was_replaced[0]
  1654.       && GET_CODE (to) == GET_CODE (x)
  1655.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  1656.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1657.       && INTVAL (XEXP (to, 1)) > 0
  1658.       && INTVAL (XEXP (x, 1)) > 0
  1659.       && (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (to, 1))
  1660.           < GET_MODE_BITSIZE (GET_MODE (x))))
  1661.     {
  1662.       if (!undobuf.storage)
  1663.         undobuf.storage = (char *) oballoc (0);
  1664.       return gen_rtx (GET_CODE (x), GET_MODE (x),
  1665.               XEXP (to, 0),
  1666.               gen_rtx (CONST_INT, VOIDmode,
  1667.                    INTVAL (XEXP (x, 1))
  1668.                    + INTVAL (XEXP (to, 1))));
  1669.     }
  1670.       break;
  1671.  
  1672.     case LSHIFT:
  1673.     case ASHIFT:
  1674. #ifdef SHIFT_COUNT_TRUNCATED
  1675.       /* (lshift <X> (sign_extend <Y>)) = (lshift <X> <Y>) (most machines).
  1676.      True for all kinds of shifts and also for zero_extend.  */
  1677.       if (was_replaced[1]
  1678.       && (GET_CODE (to) == SIGN_EXTEND
  1679.           || GET_CODE (to) == ZERO_EXTEND)
  1680.       && GET_CODE (to) == REG)
  1681.     {
  1682.       if (!undobuf.storage)
  1683.         undobuf.storage = (char *) oballoc (0);
  1684.       SUBST (XEXP (x, 1), gen_rtx (SUBREG, GET_MODE (to), XEXP (to, 0), 0));
  1685.     }
  1686. #endif
  1687.       /* (lshift (and (lshiftrt <foo> <X>) <Y>) <X>)
  1688.      happens copying between bit fields in similar structures.
  1689.      It can be replaced by one and instruction.
  1690.      It does not matter whether the shifts are logical or arithmetic.  */
  1691.       if (GET_CODE (XEXP (x, 0)) == AND
  1692.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  1693.       && INTVAL (XEXP (x, 1)) > 0
  1694.       && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
  1695.       && XEXP (XEXP (x, 0), 0) == to
  1696.       && (GET_CODE (to) == LSHIFTRT
  1697.           || GET_CODE (to) == ASHIFTRT)
  1698. #if 0
  1699. /* I now believe this restriction is unnecessary.
  1700.    The outer shift will discard those bits in any case, right?  */
  1701.  
  1702.           /* If inner shift is arithmetic, either it shifts left or
  1703.          the bits it shifts the sign into are zeroed by the and.  */
  1704.           && (INTVAL (XEXP (x, 1)) < 0
  1705.               || ((unsigned) INTVAL (XEXP (XEXP (x, 0), 1))
  1706.               < 1 << (GET_MODE_BITSIZE (GET_MODE (x))
  1707.                   - INTVAL (XEXP (x, 0)))))
  1708. #endif
  1709.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1710.       && INTVAL (XEXP (x, 1)) == INTVAL (XEXP (to, 1)))
  1711.     {
  1712.       if (!undobuf.storage)
  1713.         undobuf.storage = (char *) oballoc (0);
  1714.       /* The constant in the new `and' is <Y> << <X>
  1715.          but clear out all bits that don't belong in our mode.  */
  1716.       return gen_rtx (AND, GET_MODE (x), XEXP (to, 0),
  1717.               gen_rtx (CONST_INT, VOIDmode,
  1718.                    (GET_MODE_MASK (GET_MODE (x))
  1719.                     & ((GET_MODE_MASK (GET_MODE (x))
  1720.                     & INTVAL (XEXP (XEXP (x, 0), 1)))
  1721.                        << INTVAL (XEXP (x, 1))))));
  1722.     } 
  1723.       /* Two shifts in a row in same direction with constant counts
  1724.      may be combined.  */
  1725.       if (was_replaced[0]
  1726.       && (GET_CODE (to) == ASHIFT || GET_CODE (to) == LSHIFT)
  1727.       && GET_CODE (XEXP (x, 1)) == CONST_INT
  1728.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1729.       && INTVAL (XEXP (to, 1)) > 0
  1730.       && INTVAL (XEXP (x, 1)) > 0
  1731.       && (INTVAL (XEXP (x, 1)) + INTVAL (XEXP (to, 1))
  1732.           < GET_MODE_BITSIZE (GET_MODE (x))))
  1733.     {
  1734.       if (!undobuf.storage)
  1735.         undobuf.storage = (char *) oballoc (0);
  1736.       return gen_rtx (GET_CODE (x), GET_MODE (x),
  1737.               XEXP (to, 0),
  1738.               gen_rtx (CONST_INT, VOIDmode,
  1739.                    INTVAL (XEXP (x, 1))
  1740.                    + INTVAL (XEXP (to, 1))));
  1741.     }
  1742.       /* (ashift (ashiftrt <foo> <X>) <X>)
  1743.      (or, on some machines, (ashift (ashift <foo> <-X>) <X>) instead)
  1744.      happens if you divide by 2**N and then multiply by 2**N.
  1745.      It can be replaced by one `and' instruction.
  1746.      It does not matter whether the shifts are logical or arithmetic.  */
  1747.       if (GET_CODE (XEXP (x, 1)) == CONST_INT
  1748.       && INTVAL (XEXP (x, 1)) > 0
  1749.       && was_replaced[0]
  1750.       && (((GET_CODE (to) == LSHIFTRT || GET_CODE (to) == ASHIFTRT)
  1751.            && GET_CODE (XEXP (to, 1)) == CONST_INT
  1752.            && INTVAL (XEXP (x, 1)) == INTVAL (XEXP (to, 1)))
  1753.           ||
  1754.           ((GET_CODE (to) == LSHIFT || GET_CODE (to) == ASHIFT)
  1755.            && GET_CODE (XEXP (to, 1)) == CONST_INT
  1756.            && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (to, 1)))))
  1757.     {
  1758.       if (!undobuf.storage)
  1759.         undobuf.storage = (char *) oballoc (0);
  1760.       /* The constant in the new `and' is -1 << <X>
  1761.          but clear out all bits that don't belong in our mode.  */
  1762.       return gen_rtx (AND, GET_MODE (x), XEXP (to, 0),
  1763.               gen_rtx (CONST_INT, VOIDmode,
  1764.                    (GET_MODE_MASK (GET_MODE (x))
  1765.                     & (GET_MODE_MASK (GET_MODE (x))
  1766.                        << INTVAL (XEXP (x, 1))))));
  1767.     } 
  1768.  
  1769.     }
  1770.  
  1771.   return x;
  1772. }
  1773.  
  1774. /* This is the AND case of the function subst.  */
  1775.  
  1776. static rtx
  1777. simplify_and_const_int (x, to)
  1778.      rtx x, to;
  1779. {
  1780.   register rtx varop = XEXP (x, 0);
  1781.   register int constop = INTVAL (XEXP (x, 1));
  1782.  
  1783.   /* (and (subreg (and <foo> <constant>) 0) <constant>)
  1784.      results from an andsi followed by an andqi,
  1785.      which happens frequently when storing bit-fields
  1786.      on something whose result comes from an andsi.  */
  1787.   if (GET_CODE (varop) == SUBREG
  1788.       && XEXP (varop, 0) == to
  1789.       && subreg_lowpart_p (varop)
  1790.       && GET_CODE (to) == AND
  1791.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1792.       /* Verify that the result of the outer `and'
  1793.      is not affected by any bits not defined in the inner `and'.
  1794.      True if the outer mode is narrower, or if the outer constant
  1795.      masks to zero all the bits that the inner mode doesn't have.  */
  1796.       && (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (GET_MODE (to))
  1797.       || (constop & ~ GET_MODE_MASK (GET_MODE (to))) == 0))
  1798.     {
  1799.       if (!undobuf.storage)
  1800.     undobuf.storage = (char *) oballoc (0);
  1801.       return gen_rtx (AND, GET_MODE (x),
  1802.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1803.               gen_rtx (CONST_INT, VOIDmode,
  1804.                    constop
  1805.                    /* Remember that the bits outside that mode
  1806.                   are not being changed, so the effect
  1807.                   is as if they were all 1.  */
  1808.                    & INTVAL (XEXP (to, 1))));
  1809.     } 
  1810.   /* (and:SI (zero_extract:SI ...) <constant>)
  1811.      results from an andsi following a byte-fetch on risc machines.
  1812.      When the constant includes all bits extracted, eliminate the `and'.  */
  1813.   if (GET_CODE (varop) == ZERO_EXTRACT
  1814.       && GET_CODE (XEXP (varop, 1)) == CONST_INT
  1815.       /* The `and' must not clear any bits that the extract can give.  */
  1816.       && (~ constop & ((1 << INTVAL (XEXP (varop, 1))) - 1)) == 0)
  1817.     return varop;
  1818.   /* (and (zero_extend <foo>) <constant>)
  1819.      often results from storing in a bit-field something
  1820.      that was calculated as a short.  Replace with a single `and'
  1821.      in whose constant all bits not in <foo>'s mode are zero.  */
  1822.   if (varop == to
  1823.       && GET_CODE (to) == ZERO_EXTEND
  1824.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
  1825.     {
  1826.       if (!undobuf.storage)
  1827.     undobuf.storage = (char *) oballoc (0);
  1828.       return gen_rtx (AND, GET_MODE (x),
  1829.               /* This is a perverse SUBREG, wider than its base.  */
  1830.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1831.               gen_rtx (CONST_INT, VOIDmode,
  1832.                    constop & GET_MODE_MASK (GET_MODE (XEXP (to, 0)))));
  1833.     }
  1834.   /* (and (sign_extend <foo>) <constant>)
  1835.      can be replaced with (and (subreg <foo>) <constant>)
  1836.      if <constant> is narrower than <foo>'s mode,
  1837.      or with (zero_extend <foo>) if <constant> is a mask for that mode.  */
  1838.   if (varop == to
  1839.       && GET_CODE (to) == SIGN_EXTEND
  1840.       && ((unsigned) constop <= GET_MODE_MASK (GET_MODE (XEXP (to, 0))))
  1841.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
  1842.     {
  1843.       if (!undobuf.storage)
  1844.     undobuf.storage = (char *) oballoc (0);
  1845.       if (constop == GET_MODE_MASK (GET_MODE (XEXP (to, 0))))
  1846.     return gen_rtx (ZERO_EXTEND, GET_MODE (x), XEXP (to, 0));
  1847.       return gen_rtx (AND, GET_MODE (x),
  1848.               /* This is a perverse SUBREG, wider than its base.  */
  1849.               gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)),
  1850.               XEXP (x, 1));
  1851.     }
  1852.   /* (and (and <foo> <constant>) <constant>)
  1853.      comes from two and instructions in a row.  */
  1854.   if (varop == to
  1855.       && GET_CODE (to) == AND
  1856.       && GET_CODE (XEXP (to, 1)) == CONST_INT)
  1857.     {
  1858.       if (!undobuf.storage)
  1859.     undobuf.storage = (char *) oballoc (0);
  1860.       return gen_rtx (AND, GET_MODE (x),
  1861.               XEXP (to, 0),
  1862.               gen_rtx (CONST_INT, VOIDmode,
  1863.                    constop
  1864.                    & INTVAL (XEXP (to, 1))));
  1865.     }
  1866.   /* (and (ashiftrt (ashift FOO N) N) CONST)
  1867.      may be simplified to (and FOO CONST) if CONST masks off the bits
  1868.      changed by the two shifts.  */
  1869.   if (GET_CODE (varop) == ASHIFTRT
  1870.       && GET_CODE (XEXP (varop, 1)) == CONST_INT
  1871.       && XEXP (varop, 0) == to
  1872.       && GET_CODE (to) == ASHIFT
  1873.       && GET_CODE (XEXP (to, 1)) == CONST_INT
  1874.       && INTVAL (XEXP (varop, 1)) == INTVAL (XEXP (to, 1))
  1875.       && ((unsigned) constop >> INTVAL (XEXP (varop, 1))) == 0)
  1876.     {
  1877.       if (!undobuf.storage)
  1878.     undobuf.storage = (char *) oballoc (0);
  1879.       /* If CONST is a mask for the low byte,
  1880.      change this into a zero-extend instruction
  1881.      from just the low byte of FOO.  */
  1882.       if (constop == GET_MODE_MASK (QImode))
  1883.     {
  1884.       rtx temp = gen_lowpart_for_combine (QImode, XEXP (to, 0));
  1885.       if (GET_CODE (temp) != CLOBBER)
  1886.         return gen_rtx (ZERO_EXTEND, GET_MODE (x), temp);
  1887.     }
  1888.       return gen_rtx (AND, GET_MODE (x),
  1889.               XEXP (to, 0), XEXP (x, 1));
  1890.     }
  1891.   /* (and (ashiftrt (zero_extend FOO) N) CONST)
  1892.      may be simplified to (and (ashiftrt (subreg FOO) N) CONST)
  1893.      if CONST masks off the bits changed by extension.  */
  1894.   if ((GET_CODE (varop) == ASHIFTRT || GET_CODE (varop) == LSHIFTRT)
  1895.       && GET_CODE (XEXP (varop, 1)) == CONST_INT
  1896.       && XEXP (varop, 0) == to
  1897.       && (GET_CODE (to) == ZERO_EXTEND || GET_CODE (to) == SIGN_EXTEND)
  1898.       /* Verify the and discards all the extended bits.  */
  1899.       && (((unsigned) constop << INTVAL (XEXP (varop, 1)))
  1900.       >> GET_MODE_BITSIZE (GET_MODE (XEXP (to, 0)))) == 0
  1901.       && FAKE_EXTEND_SAFE_P (GET_MODE (x), XEXP (to, 0)))
  1902.     {
  1903.       if (!undobuf.storage)
  1904.     undobuf.storage = (char *) oballoc (0);
  1905.       SUBST (XEXP (varop, 0),
  1906.          gen_lowpart_for_combine (GET_MODE (x), XEXP (to, 0)));
  1907.       return x;
  1908.     }
  1909.   /* (and x const) may be converted to (zero_extend (subreg x 0)).  */
  1910.   if (constop == GET_MODE_MASK (QImode)
  1911.       && GET_CODE (varop) == REG)
  1912.     {
  1913.       if (!undobuf.storage)
  1914.     undobuf.storage = (char *) oballoc (0);
  1915.       return gen_rtx (ZERO_EXTEND, GET_MODE (x),
  1916.               gen_rtx (SUBREG, QImode, varop, 0));
  1917.     }
  1918.   if (constop == GET_MODE_MASK (HImode)
  1919.       && GET_CODE (varop) == REG)
  1920.     {
  1921.       if (!undobuf.storage)
  1922.     undobuf.storage = (char *) oballoc (0);
  1923.       return gen_rtx (ZERO_EXTEND, GET_MODE (x),
  1924.               gen_rtx (SUBREG, HImode, varop, 0));
  1925.     }
  1926.   /* No simplification applies.  */
  1927.   return 0;
  1928. }
  1929.  
  1930. /* Like gen_lowpart but for use by combine.  In combine it is not possible
  1931.    to create any new pseudoregs.  However, it is safe to create
  1932.    invalid memory addresses, because combine will try to recognize
  1933.    them and all they will do is make the combine attempt fail.
  1934.  
  1935.    If for some reason this cannot do its job, an rtx
  1936.    (clobber (const_int 0)) is returned.
  1937.    An insn containing that will not be recognized.  */
  1938.  
  1939. #undef gen_lowpart
  1940.  
  1941. static rtx
  1942. gen_lowpart_for_combine (mode, x)
  1943.      enum machine_mode mode;
  1944.      register rtx x;
  1945. {
  1946.   if (GET_CODE (x) == SUBREG || GET_CODE (x) == REG)
  1947.     return gen_lowpart (mode, x);
  1948.   if (GET_MODE (x) == mode)
  1949.     return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1950.   if (GET_CODE (x) == MEM)
  1951.     {
  1952.       register int offset = 0;
  1953.  
  1954.       /* Refuse to work on a volatile memory ref.  */
  1955.       if (MEM_VOLATILE_P (x))
  1956.     return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1957.  
  1958.       /* If we want to refer to something bigger than the original memref,
  1959.      generate a perverse subreg instead.  That will force a reload
  1960.      of the original memref X.  */
  1961.       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
  1962.     return gen_rtx (SUBREG, mode, x, 0);
  1963.  
  1964. #ifdef WORDS_BIG_ENDIAN
  1965.       offset = (max (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
  1966.         - max (GET_MODE_SIZE (mode), UNITS_PER_WORD));
  1967. #endif
  1968. #ifdef BYTES_BIG_ENDIAN
  1969.       /* Adjust the address so that the address-after-the-data
  1970.      is unchanged.  */
  1971.       offset -= (min (UNITS_PER_WORD, GET_MODE_SIZE (mode))
  1972.          - min (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
  1973. #endif
  1974.       return gen_rtx (MEM, mode, plus_constant (XEXP (x, 0),
  1975.                         offset));
  1976.     }
  1977.   else
  1978.     return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
  1979. }
  1980.  
  1981. /* After substitution, if the resulting pattern looks like
  1982.    (set (cc0) (and ...)) or (set (cc0) (lshiftrt ...)),
  1983.    this function is called to simplify the
  1984.    pattern into a bit-field operation if possible.  */
  1985.  
  1986. static void
  1987. simplify_set_cc0_and (insn)
  1988.      rtx insn;
  1989. {
  1990.   register rtx value = XEXP (PATTERN (insn), 1);
  1991.   register rtx op0 = XEXP (value, 0);
  1992.   register rtx op1 = XEXP (value, 1);
  1993.   int offset = 0;
  1994.   rtx var = 0;
  1995.   rtx bitnum = 0;
  1996.   int temp;
  1997.   int unit;
  1998.   rtx newpat;
  1999.  
  2000.   if (GET_CODE (value) == AND)
  2001.     {
  2002.       op0 = XEXP (value, 0);
  2003.       op1 = XEXP (value, 1);
  2004.     }
  2005.   else if (GET_CODE (value) == LSHIFTRT)
  2006.     {
  2007.       /* If there is no AND, but there is a shift that discards
  2008.      all but the sign bit, we can pretend that the shift result
  2009.      is ANDed with 1.  Otherwise we cannot handle just a shift.  */
  2010.       if (GET_CODE (XEXP (value, 1)) == CONST_INT
  2011.       && (INTVAL (XEXP (value, 1))
  2012.           == GET_MODE_BITSIZE (GET_MODE (value)) - 1))
  2013.     {
  2014.       op0 = value;
  2015.       op1 = const1_rtx;
  2016.     }
  2017.       else
  2018.     return;
  2019.     }
  2020.   else
  2021.     abort ();
  2022.  
  2023.   /* Look for a constant power of 2 or a shifted 1
  2024.      on either side of the AND.  Set VAR to the other side.
  2025.      Set BITNUM to the shift count of the 1 (as an rtx).
  2026.      Or, if bit number is constant, set OFFSET to the bit number.  */
  2027.  
  2028.   switch (GET_CODE (op0))
  2029.     {
  2030.     case CONST_INT:
  2031.       temp = exact_log2 (INTVAL (op0));
  2032.       if (temp < 0)
  2033.     return;
  2034.       offset = temp;
  2035.       var = op1;
  2036.       break;
  2037.  
  2038.     case ASHIFT:
  2039.     case LSHIFT:
  2040.       if (XEXP (op0, 0) == const1_rtx)
  2041.     {
  2042.       bitnum = XEXP (op0, 1);
  2043.       var = op1;
  2044.     }
  2045.     }
  2046.   if (var == 0)
  2047.     switch (GET_CODE (op1))
  2048.       {
  2049.       case CONST_INT:
  2050.     temp = exact_log2 (INTVAL (op1));
  2051.     if (temp < 0)
  2052.       return;
  2053.     offset = temp;
  2054.     var = op0;
  2055.     break;
  2056.  
  2057.       case ASHIFT:
  2058.       case LSHIFT:
  2059.     if (XEXP (op1, 0) == const1_rtx)
  2060.       {
  2061.         bitnum = XEXP (op1, 1);
  2062.         var = op0;
  2063.       }
  2064.       }
  2065.  
  2066.   /* If VAR is 0, we didn't find something recognizable.  */
  2067.   if (var == 0)
  2068.     return;
  2069.  
  2070.   if (!undobuf.storage)
  2071.     undobuf.storage = (char *) oballoc (0);
  2072.  
  2073.   /* If the bit position is currently exactly 0,
  2074.      extract a right-shift from the variable portion.  */
  2075.   if (offset == 0
  2076.       && (GET_CODE (var) == ASHIFTRT || GET_CODE (var) == LSHIFTRT))
  2077.     {
  2078.       bitnum = XEXP (var, 1);
  2079.       var = XEXP (var, 0);
  2080.     }
  2081.  
  2082.   if (GET_CODE (var) == SUBREG && SUBREG_WORD (var) == 0)
  2083.     var = SUBREG_REG (var);
  2084.  
  2085.   /* Note that BITNUM and OFFSET are always little-endian thru here
  2086.      even on a big-endian machine.  */
  2087.  
  2088. #ifdef BITS_BIG_ENDIAN
  2089.   unit = GET_MODE_BITSIZE (GET_MODE (var)) - 1;
  2090.  
  2091.   if (bitnum != 0)
  2092.     bitnum = gen_rtx (MINUS, SImode,
  2093.               gen_rtx (CONST_INT, VOIDmode, unit), bitnum);
  2094.   else
  2095.     offset = unit - offset;
  2096. #endif
  2097.  
  2098.   if (bitnum == 0)
  2099.     bitnum = gen_rtx (CONST_INT, VOIDmode, offset);
  2100.  
  2101.   newpat = gen_rtx (SET, VOIDmode, cc0_rtx,
  2102.             gen_rtx (ZERO_EXTRACT, VOIDmode, var, const1_rtx, bitnum));
  2103.   if (recog (newpat, insn) >= 0)
  2104.     {
  2105.       if (undobuf.num_undo < MAX_UNDO)
  2106.     {
  2107.       undobuf.undo[undobuf.num_undo].where = &XEXP (PATTERN (insn), 1);
  2108.       undobuf.undo[undobuf.num_undo].old_contents = value;
  2109.       XEXP (PATTERN (insn), 1) = XEXP (newpat, 1);
  2110.     }
  2111.       undobuf.num_undo++;
  2112.     }
  2113. }
  2114.  
  2115. /* Update the records of when each REG was most recently set or killed
  2116.    for the things done by INSN.  This is the last thing done in processing
  2117.    INSN in the combiner loop.
  2118.  
  2119.    We update reg_last_set, reg_last_death, and also the similar information
  2120.    mem_last_set (which insn most recently modified memory)
  2121.    and last_call_cuid (which insn was the most recent subroutine call).  */
  2122.  
  2123. static void
  2124. record_dead_and_set_regs (insn)
  2125.      rtx insn;
  2126. {
  2127.   register rtx link;
  2128.   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  2129.     {
  2130.       if (REG_NOTE_KIND (link) == REG_DEAD)
  2131.     reg_last_death[REGNO (XEXP (link, 0))] = insn;
  2132.       else if (REG_NOTE_KIND (link) == REG_INC)
  2133.     reg_last_set[REGNO (XEXP (link, 0))] = insn;
  2134.     }
  2135.  
  2136.   if (GET_CODE (insn) == CALL_INSN)
  2137.     last_call_cuid = mem_last_set = INSN_CUID (insn);
  2138.  
  2139.   if (GET_CODE (PATTERN (insn)) == PARALLEL)
  2140.     {
  2141.       register int i;
  2142.       for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
  2143.     {
  2144.       register rtx elt = XVECEXP (PATTERN (insn), 0, i);
  2145.       register enum rtx_code code = GET_CODE (elt);
  2146.       if (code == SET || code == CLOBBER)
  2147.         {
  2148.           rtx dest = XEXP (elt, 0);
  2149.           while (GET_CODE (dest) == SUBREG
  2150.              || GET_CODE (dest) == STRICT_LOW_PART
  2151.              || GET_CODE (dest) == SIGN_EXTRACT
  2152.              || GET_CODE (dest) == ZERO_EXTRACT)
  2153.         dest = XEXP (dest, 0);
  2154.           
  2155.           if (GET_CODE (dest) == REG)
  2156.         reg_last_set[REGNO (dest)] = insn;
  2157.           else if (GET_CODE (dest) == MEM)
  2158.         mem_last_set = INSN_CUID (insn);
  2159.         }
  2160.     }
  2161.     }
  2162.   else if (GET_CODE (PATTERN (insn)) == SET
  2163.        || GET_CODE (PATTERN (insn)) == CLOBBER)
  2164.     {
  2165.       register rtx dest = XEXP (PATTERN (insn), 0);
  2166.  
  2167.       while (GET_CODE (dest) == SUBREG
  2168.          || GET_CODE (dest) == STRICT_LOW_PART
  2169.          || GET_CODE (dest) == SIGN_EXTRACT
  2170.          || GET_CODE (dest) == ZERO_EXTRACT)
  2171.     dest = XEXP (dest, 0);
  2172.  
  2173.       if (GET_CODE (dest) == REG)
  2174.     reg_last_set[REGNO (dest)] = insn;
  2175.       else if (GET_CODE (dest) == MEM)
  2176.     mem_last_set = INSN_CUID (insn);
  2177.     }
  2178. }
  2179.  
  2180. /* Return nonzero if expression X refers to a REG or to memory
  2181.    that is set in an instruction more recent than FROM_CUID.  */
  2182.  
  2183. static int
  2184. use_crosses_set_p (x, from_cuid)
  2185.      register rtx x;
  2186.      int from_cuid;
  2187. {
  2188.   register char *fmt;
  2189.   register int i;
  2190.   register enum rtx_code code = GET_CODE (x);
  2191.  
  2192.   if (code == REG)
  2193.     {
  2194.       register int regno = REGNO (x);
  2195. #ifdef PUSH_ROUNDING
  2196.       /* Don't allow uses of the stack pointer to be moved,
  2197.      because we don't know whether the move crosses a push insn.  */
  2198.       if (regno == STACK_POINTER_REGNUM)
  2199.     return 1;
  2200. #endif
  2201.       return (reg_last_set[regno]
  2202.           && INSN_CUID (reg_last_set[regno]) > from_cuid);
  2203.     }
  2204.  
  2205.   if (code == MEM && mem_last_set > from_cuid)
  2206.     return 1;
  2207.  
  2208.   fmt = GET_RTX_FORMAT (code);
  2209.  
  2210.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2211.     {
  2212.       if (fmt[i] == 'E')
  2213.     {
  2214.       register int j;
  2215.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  2216.         if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
  2217.           return 1;
  2218.     }
  2219.       else if (fmt[i] == 'e'
  2220.            && use_crosses_set_p (XEXP (x, i), from_cuid))
  2221.     return 1;
  2222.     }
  2223.   return 0;
  2224. }
  2225.  
  2226. /* Return nonzero if reg REGNO is marked as dying in INSN.  */
  2227.  
  2228. int
  2229. regno_dead_p (regno, insn)
  2230.      int regno;
  2231.      rtx insn;
  2232. {
  2233.   register rtx link;
  2234.  
  2235.   for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  2236.     if ((REG_NOTE_KIND (link) == REG_DEAD
  2237.      || REG_NOTE_KIND (link) == REG_INC)
  2238.     && REGNO (XEXP (link, 0)) == regno)
  2239.       return 1;
  2240.  
  2241.   return 0;
  2242. }
  2243.  
  2244. /* Return nonzero if J is the first insn following I,
  2245.    not counting labels, line numbers, etc.
  2246.    We assume that J follows I.  */
  2247.  
  2248. static int
  2249. adjacent_insns_p (i, j)
  2250.      rtx i, j;
  2251. {
  2252.   register rtx insn;
  2253.   for (insn = NEXT_INSN (i); insn != j; insn = NEXT_INSN (insn))
  2254.     if (GET_CODE (insn) == INSN
  2255.     || GET_CODE (insn) == CALL_INSN
  2256.     || GET_CODE (insn) == JUMP_INSN)
  2257.       return 0;
  2258.   return 1;
  2259. }
  2260.  
  2261. /* Check that X is an insn-body for an `asm' with operands
  2262.    and that the operands mentioned in it are legitimate.  */
  2263.  
  2264. static int
  2265. check_asm_operands (x)
  2266.      rtx x;
  2267. {
  2268.   int noperands = asm_noperands (x);
  2269.   rtx *operands;
  2270.   int i;
  2271.  
  2272.   if (noperands < 0)
  2273.     return 0;
  2274.   if (noperands == 0)
  2275.     return 1;
  2276.  
  2277.   operands = (rtx *) alloca (noperands * sizeof (rtx));
  2278.   decode_asm_operands (x, operands, 0, 0, 0);
  2279.  
  2280.   for (i = 0; i < noperands; i++)
  2281.     if (!general_operand (operands[i], VOIDmode))
  2282.       return 0;
  2283.  
  2284.   return 1;
  2285. }
  2286.  
  2287. /* Concatenate the list of logical links of OINSN
  2288.    into INSN's list of logical links.
  2289.    Modifies OINSN destructively.
  2290.  
  2291.    If ALL_LINKS is nonzero, move all the links that OINSN has.
  2292.    Otherwise, move only those that point to insns that set regs
  2293.    that die in the insn OINSN.
  2294.    Other links are clobbered so that they are no longer effective.  */
  2295.  
  2296. static void
  2297. add_links (insn, oinsn, all_links)
  2298.      rtx insn, oinsn;
  2299.      int all_links;
  2300. {
  2301.   register rtx links = LOG_LINKS (oinsn);
  2302.   if (! all_links)
  2303.     {
  2304.       rtx tail;
  2305.       for (tail = links; tail; tail = XEXP (tail, 1))
  2306.     {
  2307.       rtx target = XEXP (tail, 0);
  2308.       if (GET_CODE (target) != INSN
  2309.           || GET_CODE (PATTERN (target)) != SET
  2310.           || GET_CODE (SET_DEST (PATTERN (target))) != REG
  2311.           || ! dead_or_set_p (oinsn, SET_DEST (PATTERN (target))))
  2312.         /* OINSN is going to become a NOTE 
  2313.            so a link pointing there will have no effect.  */
  2314.         XEXP (tail, 0) = oinsn;
  2315.     }
  2316.     }
  2317.   if (LOG_LINKS (insn) == 0)
  2318.     LOG_LINKS (insn) = links;
  2319.   else
  2320.     {
  2321.       register rtx next, prev = LOG_LINKS (insn);
  2322.       while (next = XEXP (prev, 1))
  2323.     prev = next;
  2324.       XEXP (prev, 1) = links;
  2325.     }
  2326. }
  2327.   
  2328. /* Delete any LOG_LINKS of INSN which point at OINSN.  */
  2329.  
  2330. static void
  2331. remove_links (insn, oinsn)
  2332.      rtx insn, oinsn;
  2333. {
  2334.   register rtx next = LOG_LINKS (insn), prev = 0;
  2335.   while (next)
  2336.     {
  2337.       if (XEXP (next, 0) == oinsn)
  2338.     {
  2339.       if (prev)
  2340.         XEXP (prev, 1) = XEXP (next, 1);
  2341.       else
  2342.         LOG_LINKS (insn) = XEXP (next, 1);
  2343.     }
  2344.       else
  2345.     prev = next;
  2346.       next = XEXP (next, 1);
  2347.     }
  2348. }
  2349.  
  2350. /* Concatenate the any elements of the list of reg-notes INCS
  2351.    which are of type REG_INC
  2352.    into INSN's list of reg-notes.  */
  2353.  
  2354. static void
  2355. add_incs (insn, incs)
  2356.      rtx insn, incs;
  2357. {
  2358.   register rtx tail;
  2359.  
  2360.   for (tail = incs; tail; tail = XEXP (tail, 1))
  2361.     if (REG_NOTE_KIND (tail) == REG_INC)
  2362.       REG_NOTES (insn)
  2363.     = gen_rtx (EXPR_LIST, REG_INC, XEXP (tail, 0), REG_NOTES (insn));
  2364. }
  2365.  
  2366. /* Remove register number REGNO from the dead registers list of INSN.  */
  2367.  
  2368. void
  2369. remove_death (regno, insn)
  2370.      int regno;
  2371.      rtx insn;
  2372. {
  2373.   register rtx link, next;
  2374.   while ((link = REG_NOTES (insn))
  2375.      && REG_NOTE_KIND (link) == REG_DEAD
  2376.      && REGNO (XEXP (link, 0)) == regno)
  2377.     REG_NOTES (insn) = XEXP (link, 1);
  2378.  
  2379.   if (link)
  2380.     while (next = XEXP (link, 1))
  2381.       {
  2382.     if (REG_NOTE_KIND (next) == REG_DEAD
  2383.         && REGNO (XEXP (next, 0)) == regno)
  2384.       XEXP (link, 1) = XEXP (next, 1);
  2385.     else
  2386.       link = next;
  2387.       }
  2388. }
  2389.  
  2390. /* For each register (hardware or pseudo) used within expression X,
  2391.    if its death is in an instruction with cuid
  2392.    between FROM_CUID (inclusive) and TO_INSN (exclusive),
  2393.    mark it as dead in TO_INSN instead.
  2394.  
  2395.    This is done when X is being merged by combination into TO_INSN.  */
  2396.  
  2397. static void
  2398. move_deaths (x, from_cuid, to_insn)
  2399.      rtx x;
  2400.      int from_cuid;
  2401.      rtx to_insn;
  2402. {
  2403.   register char *fmt;
  2404.   register int len, i;
  2405.   register enum rtx_code code = GET_CODE (x);
  2406.  
  2407.   if (code == REG)
  2408.     {
  2409.       register rtx where_dead = reg_last_death[REGNO (x)];
  2410.  
  2411.       if (where_dead && INSN_CUID (where_dead) >= from_cuid
  2412.       && INSN_CUID (where_dead) < INSN_CUID (to_insn))
  2413.     {
  2414.       remove_death (REGNO (x), reg_last_death[REGNO (x)]);
  2415.       if (! dead_or_set_p (to_insn, x))
  2416.         REG_NOTES (to_insn)
  2417.           = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (to_insn));
  2418.     }
  2419.       return;
  2420.     }
  2421.  
  2422.   len = GET_RTX_LENGTH (code);
  2423.   fmt = GET_RTX_FORMAT (code);
  2424.  
  2425.   for (i = 0; i < len; i++)
  2426.     {
  2427.       if (fmt[i] == 'E')
  2428.     {
  2429.       register int j;
  2430.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  2431.         move_deaths (XVECEXP (x, i, j), from_cuid, to_insn);
  2432.     }
  2433.       else if (fmt[i] == 'e')
  2434.     move_deaths (XEXP (x, i), from_cuid, to_insn);
  2435.     }
  2436. }
  2437.  
  2438. /* Like move_deaths, but deaths are moving both forward
  2439.    (from FROM_CUID to TO_INSN), and backwards
  2440.    (from FROM_INSN to TO_INSN).  This is what happens
  2441.    when an insn is removed after applying the distributive law.  */
  2442.  
  2443. static void
  2444. move_deaths_2 (x, from_cuid, from_insn, to_insn)
  2445.      rtx x;
  2446.      int from_cuid;
  2447.      rtx from_insn, to_insn;
  2448. {
  2449.   register char *fmt;
  2450.   register int len, i;
  2451.   register enum rtx_code code = GET_CODE (x);
  2452.  
  2453.   if (code == REG)
  2454.     {
  2455.       register rtx where_dead = reg_last_death[REGNO (x)];
  2456.  
  2457.       if (where_dead && INSN_CUID (where_dead) >= from_cuid
  2458.       && INSN_CUID (where_dead) < INSN_CUID (to_insn))
  2459.     {
  2460.       remove_death (REGNO (x), reg_last_death[REGNO (x)]);
  2461.       if (! dead_or_set_p (to_insn, x))
  2462.         REG_NOTES (to_insn)
  2463.           = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (to_insn));
  2464.     }
  2465.       /* Can't use where_dead for from_insn because it has
  2466.      not been computed yet.  */
  2467.       else if (dead_or_set_p (from_insn, x))
  2468.     {
  2469.       remove_death (REGNO (x), from_insn);
  2470.       if (! dead_or_set_p (to_insn, x))
  2471.         REG_NOTES (to_insn)
  2472.           = gen_rtx (EXPR_LIST, REG_DEAD, x, REG_NOTES (to_insn));
  2473.     }
  2474.       return;
  2475.     }
  2476.  
  2477.   len = GET_RTX_LENGTH (code);
  2478.   fmt = GET_RTX_FORMAT (code);
  2479.  
  2480.   for (i = 0; i < len; i++)
  2481.     {
  2482.       if (fmt[i] == 'E')
  2483.     {
  2484.       register int j;
  2485.       for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  2486.         move_deaths_2 (XVECEXP (x, i, j), from_cuid, from_insn, to_insn);
  2487.     }
  2488.       else if (fmt[i] == 'e')
  2489.     move_deaths_2 (XEXP (x, i), from_cuid, from_insn, to_insn);
  2490.     }
  2491. }
  2492.  
  2493. /* The distrib combiner rewrites groups of insns so that optimizations
  2494.    can be more easily recognized.  The front-end does not know how to
  2495.    group certain kinds of operations for efficient execution, and the
  2496.    resulting code can be quite poor.  For example, on a machine without
  2497.    bitfield instructions, bitfield references look like
  2498.  
  2499.     (and (lshiftrt ... n) m)
  2500.  
  2501.    When combining two bitfield operations, such as with ||, this can
  2502.    yield code like
  2503.  
  2504.     (set z
  2505.          (or (and (lshiftrt x n) 1)
  2506.          (and (lshiftrt y n) 1)))
  2507.  
  2508.    which can be more efficiently executed as
  2509.  
  2510.     (set z
  2511.          (lshiftrt (and (or x y)
  2512.                 (1 << m)) n))
  2513.  
  2514.    From there, the combiner attempts to rewrite the insns,
  2515.    keeping flow information accurate for later passes,
  2516.    and reducing the total number of insns executed.
  2517.  
  2518.    This function returns the point at which we should try
  2519.    looking for more simplifications.  This will be before
  2520.    INSN if the call succeeds.  We do not need to fear
  2521.    infinite loops, since this function is guaranteed to
  2522.    eliminate at least one (non-note) instruction if it returns
  2523.    successfully.  */
  2524.  
  2525. static rtx
  2526. try_distrib (insn, xprev1, xprev2)
  2527.      rtx insn, xprev1, xprev2;
  2528. {
  2529.   rtx pat = PATTERN (insn);
  2530.   rtx prev1, prev2, pat1, pat2, src1, src2;
  2531.   rtx to_prev, to_insn;
  2532.   enum rtx_code code;
  2533.   int insn_code_number, prev_code_number, regno;
  2534.   rtx new_insn_pat, new_prev_pat;
  2535.  
  2536.   distrib_attempts++;
  2537.  
  2538.   /* ??? Need to implement a test that PREV2 and PREV1
  2539.      are completely independent.  Right now their
  2540.      recognition ability is sufficiently limited that
  2541.      it should not be necessary, but better safe than sorry.  */
  2542.  
  2543.   /* Let PREV1 be the later of the two insns, and PREV2 the earlier.  */
  2544.   if (INSN_CUID (xprev1) > INSN_CUID (xprev2))
  2545.     {
  2546.       prev1 = xprev1;
  2547.       prev2 = xprev2;
  2548.     }
  2549.   else
  2550.     {
  2551.       prev1 = xprev2;
  2552.       prev2 = xprev1;
  2553.     }
  2554.  
  2555.   pat1 = PATTERN (prev1);
  2556.   pat2 = PATTERN (prev2);
  2557.  
  2558.   /* First, see if INSN, PREV1, and PREV2 have patterns we can expect
  2559.      to simplify.  */
  2560.  
  2561.   if (GET_CODE (pat) != SET
  2562.       || GET_CODE (pat1) != SET
  2563.       || GET_CODE (pat2) != SET)
  2564.     return 0;
  2565.  
  2566.   code = GET_CODE (SET_SRC (pat));
  2567.   src1 = SET_SRC (pat1);
  2568.   src2 = SET_SRC (pat2);
  2569.  
  2570.   if (GET_CODE (SET_DEST (pat1)) != REG
  2571.       || GET_CODE (SET_DEST (pat2)) != REG)
  2572.     return 0;
  2573.  
  2574.   switch (code)
  2575.     {
  2576.     default:
  2577.       return 0;
  2578.  
  2579.     case IOR:
  2580.     case AND:
  2581.     case XOR:
  2582.     case PLUS:
  2583.       ;
  2584.     }
  2585.  
  2586.   /* Insns PREV1 and PREV2 must provide the two operands of the arithmetic
  2587.      that is done in INSN.  */
  2588.   if (! ((XEXP (SET_SRC (pat), 0) == SET_DEST (pat1)
  2589.       && XEXP (SET_SRC (pat), 1) == SET_DEST (pat2))
  2590.      ||
  2591.      (XEXP (SET_SRC (pat), 0) == SET_DEST (pat2)
  2592.       && XEXP (SET_SRC (pat), 1) == SET_DEST (pat1))))
  2593.     return 0;
  2594.  
  2595.   /* They must not be used in any other way in INSN.
  2596.      In particular, they must not be used in a result memory address.  */
  2597.   if (reg_mentioned_p (SET_DEST (pat1), SET_DEST (pat))
  2598.       || reg_mentioned_p (SET_DEST (pat2), SET_DEST (pat)))
  2599.     return 0;
  2600.  
  2601.   /* Give up if the two operands' modes don't match.  */
  2602.   if (GET_MODE (src1) != GET_MODE (src2))
  2603.     return 0;
  2604.  
  2605.   /* PREV1 and PREV2 must compute the same operation.
  2606.      Actually, there are other cases that could be handled,
  2607.      but are not implemented.  For example:
  2608.  
  2609.      (set (reg:SI 94)
  2610.       (and:SI (reg:SI 73)
  2611.           (const_int 223)))
  2612.  
  2613.      (set (reg:SI 95)
  2614.       (zero_extend:SI (subreg:QI (reg:SI 91) 0)))
  2615.  
  2616.      (set (reg:SI 96)
  2617.       (ior:SI (reg:SI 94)
  2618.           (reg:SI 95)))
  2619.  
  2620.      In this case, we know that because (reg:SI 94) has
  2621.      been anded with 223, there is no need to zero_extend
  2622.      (reg:SI 91), and we could eliminate (reg:SI 95).  */
  2623.  
  2624.   if (GET_CODE (src1) != GET_CODE (src2))
  2625.     return 0;
  2626.  
  2627.   /* The SETs in PREV1 and PREV2 do not need to be kept around.  */
  2628.  
  2629.   undobuf.num_undo = 0;
  2630.   undobuf.storage = 0;
  2631.  
  2632.   /* Substitute in the latest insn for the regs set by the earlier ones.  */
  2633.   subst_insn = insn;
  2634.   n_occurrences = 0;    /* `subst' counts here */
  2635.  
  2636.   switch (GET_CODE (src1))
  2637.     {
  2638.     /* case XOR:  Does not distribute through anything!  */
  2639.     case LSHIFTRT:
  2640.     case ASHIFTRT:
  2641.       /* Right-shift can't distribute through addition
  2642.      since the round-off would happen differently.  */
  2643.     case AND:
  2644.     case IOR:
  2645.       /* Boolean ops don't distribute through addition.  */
  2646.       if (code == PLUS)
  2647.     return 0;
  2648.  
  2649.     case LSHIFT:
  2650.     case ASHIFT:
  2651.       /* Left shifts are multiplication; they distribute through
  2652.      addition.  Also, since they work bitwise, they
  2653.      distribute through boolean operations.  */
  2654.       goto do_distrib;
  2655.  
  2656.     case MULT:
  2657.       /* Multiplication distributes through addition only.  */
  2658.       if (code != PLUS)
  2659.     return 0;
  2660.  
  2661.     do_distrib:
  2662.       if (GET_CODE (XEXP (src1, 1)) != CONST_INT
  2663.       || GET_CODE (XEXP (src2, 1)) != CONST_INT
  2664.       || INTVAL (XEXP (src1, 1)) != INTVAL (XEXP (src2, 1)))
  2665.     return 0;
  2666.  
  2667.       /* Give up if we would move a use of a reg across an alteration.
  2668.      Note this is unnecessarily conservative, since a problem really
  2669.      happens only if this reg is set *between* PREV2 and PREV1
  2670.      But this test is easier.  */
  2671.       if (use_crosses_set_p (XEXP (src2, 0), INSN_CUID (prev2)))
  2672.     return 0;
  2673.  
  2674.       /* Try changing (+ (* x c) (* y c)) to (* (+ x y) c).  */
  2675.       to_prev = gen_rtx (code, GET_MODE (src1),
  2676.              XEXP (src1, 0), XEXP (src2, 0));
  2677.       to_insn = gen_rtx (GET_CODE (src1), GET_MODE (src1), SET_DEST (pat1), XEXP (src1, 1));
  2678.       break;
  2679.  
  2680.     case ZERO_EXTEND:
  2681.     case SIGN_EXTEND:
  2682.       /* Extension can't distribute through addition;
  2683.      the carries could be changed.  */
  2684.       if (code == PLUS)
  2685.     return 0;
  2686.       {
  2687.     rtx inner1 = XEXP (src1, 0), inner2 = XEXP (src2, 0);
  2688.     int subreg_needed = 0;
  2689.  
  2690.     /* Try changing (& (extend x) (extend y)) to (extend (& x y)).  */
  2691.     /* But keep extend insns together with their subregs.  */
  2692.     if (GET_CODE (inner1) == SUBREG)
  2693.       {
  2694.         if (SUBREG_WORD (inner1) != 0)
  2695.           return 0;
  2696.         else
  2697.           {
  2698.         subreg_needed = 1;
  2699.         inner1 = SUBREG_REG (inner1);
  2700.           }
  2701.       }
  2702.  
  2703.     if (GET_CODE (inner2) == SUBREG)
  2704.       {
  2705.         if (SUBREG_WORD (inner2) != 0)
  2706.           return 0;
  2707.         else
  2708.           {
  2709.         subreg_needed = 1;
  2710.         inner2 = SUBREG_REG (inner2);
  2711.           }
  2712.       }
  2713.  
  2714.     /* Give up if we would move a use of a reg across an alteration.
  2715.        Note this is unnecessarily conservative, since a problem really
  2716.        happens only if this reg is set *between* PREV2 and PREV1
  2717.        But this test is easier.  */
  2718.     if (use_crosses_set_p (inner2, INSN_CUID (prev2)))
  2719.       return 0;
  2720.  
  2721.     to_prev = gen_rtx (code, GET_MODE (src1), inner1, inner2);
  2722.     to_insn = gen_rtx (GET_CODE (src1), GET_MODE (src1),
  2723.                subreg_needed
  2724.                ? gen_rtx (SUBREG, GET_MODE (XEXP (src1, 0)),
  2725.                       SET_DEST (pat1), 0)
  2726.                : SET_DEST (pat1));
  2727.       }
  2728.       break;
  2729.  
  2730.     default:
  2731.       return 0;
  2732.     }
  2733.  
  2734.   /* Are the results of this "substitution" a valid instruction?  */
  2735.  
  2736.   new_insn_pat = subst (PATTERN (insn), SET_SRC (PATTERN (insn)), to_insn);
  2737.   distrib_merges_1++;
  2738.  
  2739.   insn_code_number = recog (new_insn_pat, insn);
  2740.   if (insn_code_number < 0)
  2741.     {
  2742.       undo_all ();
  2743.       return 0;
  2744.     }
  2745.  
  2746.   subst_insn = prev1;
  2747.   new_prev_pat = subst (pat1, src1, to_prev);
  2748.   distrib_merges_2++;
  2749.  
  2750.   prev_code_number = recog (new_prev_pat, prev1);
  2751.   if (prev_code_number < 0)
  2752.     {
  2753.       undo_all ();
  2754.       return 0;
  2755.     }
  2756.  
  2757.   /* Everything worked; install the new patterns.  */
  2758.   INSN_CODE (insn) = insn_code_number;
  2759.   PATTERN (insn) = new_insn_pat;
  2760.  
  2761.   INSN_CODE (prev1) = prev_code_number;
  2762.   PATTERN (prev1) = new_prev_pat;
  2763.  
  2764.   /* Need to change LOG_LINKS around...PREV1 now gets
  2765.      whatever flowed into PREV2.  PREV2 is going to
  2766.      become a NOTE, so we clear out its LOG_LINKS.  */
  2767.   remove_links (insn, prev2);
  2768.   add_links (prev1, prev2, adjacent_insns_p (prev2, prev1));
  2769.  
  2770.   /* Registers which died in PREV2 now die in PREV1.
  2771.      Also, registers born in PREV2 dying in INSN now die in PREV1.  */
  2772.   move_deaths_2 (src2, INSN_CUID (prev2), insn, prev1);
  2773.  
  2774.   regno = REGNO (SET_DEST (pat2));
  2775.  
  2776.   reg_n_sets[regno]--;
  2777.   if (reg_n_sets[regno] == 0
  2778.       && ! (basic_block_live_at_start[0][regno / HOST_BITS_PER_INT]
  2779.         & (1 << (regno % HOST_BITS_PER_INT))))
  2780.     reg_n_refs[regno] = 0;
  2781.   remove_death (regno, insn);
  2782.  
  2783.   PUT_CODE (prev2, NOTE);
  2784.   NOTE_LINE_NUMBER (prev2) = NOTE_INSN_DELETED;
  2785.   NOTE_SOURCE_FILE (prev2) = 0;
  2786.  
  2787.   distrib_successes++;
  2788.   return prev1;
  2789. }
  2790.  
  2791. void
  2792. dump_combine_stats (file)
  2793.      FILE *file;
  2794. {
  2795.   fprintf
  2796.     (file,
  2797.      ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
  2798.      combine_attempts, combine_merges, combine_extras, combine_successes);
  2799.   fprintf
  2800.     (file,
  2801.      ";; Distributer statistics: %d attempts, %d:%d substitutions,\n;; %d successes.\n\n",
  2802.      distrib_attempts, distrib_merges_1,
  2803.      distrib_merges_2, distrib_successes);
  2804. }
  2805.  
  2806. void
  2807. dump_combine_total_stats (file)
  2808.      FILE *file;
  2809. {
  2810.   fprintf
  2811.     (file,
  2812.      "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
  2813.      total_attempts, total_merges, total_extras, total_successes);
  2814.   fprintf
  2815.     (file,
  2816.      "\n;; Distributer totals: %d attempts, %d:%d substitutions,\n;; %d successes.\n",
  2817.      total_distrib_attempts, total_distrib_merges_1,
  2818.      total_distrib_merges_2, total_distrib_successes);
  2819. }
  2820.